Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(taro): 添加env.js的测试用例和taro包的测试依赖 #4334

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"release:lerna": "lerna publish --force-publish=* --exact --skip-temp-tag",
"release:beta": "lerna publish --force-publish=* --exact --skip-temp-tag --preid=beta --npm-tag=beta",
"release": "npm-run-all build release:lerna && npm run changelog && node ./build/docs-version.js",
"test": "lerna run --scope eslint-plugin-taro --scope @tarojs/transformer-wx test"
"test": "lerna run --scope eslint-plugin-taro --scope @tarojs/transformer-wx --scope @tarojs/taro test"
},
"repository": {
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions packages/taro/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"@babel/preset-env", "@babel/preset-react"
]
}
58 changes: 58 additions & 0 deletions packages/taro/__tests__/env.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ENV_TYPE, getEnv } from "../src/env";

// Create a global variable in jest
const _createGlobalVarible = (name, properties) => {
Object.defineProperty(window, name, {
writable: true,
value: { ...properties }
});
};
// Destory a global variable in jest
const _clearGlobalVarible = name => {
Object.defineProperty(window, name, {
writable: true,
value: undefined
});
};

// common test flow for qq/tt/wx/swan/alipay
const _commonFlow = ({ name, properties }, fn) => {
_createGlobalVarible(name, properties);
fn();
_clearGlobalVarible(name);
};

// check getEnv function
describe("getEnv", () => {
it("is QQ ?", () =>
_commonFlow(
{ name: "qq", properties: { getSystemInfo: jest.fn() } },
() => expect(getEnv()).toBe(ENV_TYPE.QQ)
));
it("is TT ?", () =>
_commonFlow(
{ name: "tt", properties: { getSystemInfo: jest.fn() } },
() => expect(getEnv()).toBe(ENV_TYPE.TT)
));
it("is WEAPP ?", () =>
_commonFlow(
{ name: "wx", properties: { getSystemInfo: jest.fn() } },
() => expect(getEnv()).toBe(ENV_TYPE.WEAPP)
));
it("is SWAN ?", () =>
_commonFlow(
{ name: "swan", properties: { getSystemInfo: jest.fn() } },
() => expect(getEnv()).toBe(ENV_TYPE.SWAN)
));
it("is ALIPAY ?", () =>
_commonFlow(
{ name: "my", properties: { getSystemInfo: jest.fn() } },
() => expect(getEnv()).toBe(ENV_TYPE.ALIPAY)
));
it("is rn", () => {
global.__fbGenNativeModule = jest.fn();
expect(getEnv()).toBe(ENV_TYPE.RN);
global.__fbGenNativeModule = undefined;
});
it("is WEB", () => expect(getEnv()).toBe(ENV_TYPE.WEB));
});
8 changes: 7 additions & 1 deletion packages/taro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"package.json"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest",
"build": "rollup -c rollup.config.js"
},
"repository": {
Expand All @@ -30,5 +30,11 @@
"license": "MIT",
"dependencies": {
"@tarojs/utils": "1.3.15"
},
"devDependencies": {
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@types/jest": "^24.0.18",
"jest": "^24.9.0"
}
}
Loading