如何在玩笑中配置所有 cmmon 文件?
how to configure all cmmon file in jest?
如何加载jest.config
中的所有常用文件
如何加载所有通用文件和 3td 方库之类的 jquery
{
"name": "my-project",
"jest": {
setupFiles:[../src/assert]
}
}
您可以在 jest.config.js
中使用 setupFiles。将 jquery
和 moment
分配给 node.js global
对象。然后,您可以使用 global.$
和 global.moment
.
在每个测试用例中获取它们
例如
setup.js
:
const jquery = function () {
return "I'm fake jquery";
};
const moment = function () {
return "I'm fake moment";
};
global.$ = jquery;
global.moment = moment;
jest.config.js
:
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'enzyme',
setupFilesAfterEnv: [
'jest-enzyme',
'./jest.setup.js',
],
setupFiles: [
'/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/Whosebug/61727628/setup.js',
],
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
verbose: true,
};
a.test.js
:
describe('61727628', () => {
describe('a', () => {
it('should pass', () => {
console.log('global.$:', global.$);
console.log('global.moment:', global.moment);
expect(1 + 1).toBe(2);
});
});
});
b.test.js
:
describe('61727628', () => {
describe('b', () => {
it('should pass', () => {
console.log('global.$:', global.$);
console.log('global.moment:', global.moment);
expect(1 + 1).toBe(2);
});
});
});
单元测试结果:
PASS Whosebug/61727628/a.test.js
61727628
a
✓ should pass (28ms)
console.log
global.$: function () {
return "I'm fake jquery";
}
at Object.<anonymous> (Whosebug/61727628/b.test.js:4:15)
console.log
global.$: function () {
return "I'm fake jquery";
}
at Object.<anonymous> (Whosebug/61727628/a.test.js:4:15)
console.log
global.moment: function () {
return "I'm fake moment";
}
at Object.<anonymous> (Whosebug/61727628/a.test.js:5:15)
console.log
global.moment: function () {
return "I'm fake moment";
}
at Object.<anonymous> (Whosebug/61727628/b.test.js:5:15)
PASS Whosebug/61727628/b.test.js
61727628
b
✓ should pass (28ms)
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 6.026s, estimated 16s
如何加载jest.config
中的所有常用文件如何加载所有通用文件和 3td 方库之类的 jquery
{
"name": "my-project",
"jest": {
setupFiles:[../src/assert]
}
}
您可以在 jest.config.js
中使用 setupFiles。将 jquery
和 moment
分配给 node.js global
对象。然后,您可以使用 global.$
和 global.moment
.
例如
setup.js
:
const jquery = function () {
return "I'm fake jquery";
};
const moment = function () {
return "I'm fake moment";
};
global.$ = jquery;
global.moment = moment;
jest.config.js
:
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'enzyme',
setupFilesAfterEnv: [
'jest-enzyme',
'./jest.setup.js',
],
setupFiles: [
'/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/Whosebug/61727628/setup.js',
],
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
verbose: true,
};
a.test.js
:
describe('61727628', () => {
describe('a', () => {
it('should pass', () => {
console.log('global.$:', global.$);
console.log('global.moment:', global.moment);
expect(1 + 1).toBe(2);
});
});
});
b.test.js
:
describe('61727628', () => {
describe('b', () => {
it('should pass', () => {
console.log('global.$:', global.$);
console.log('global.moment:', global.moment);
expect(1 + 1).toBe(2);
});
});
});
单元测试结果:
PASS Whosebug/61727628/a.test.js
61727628
a
✓ should pass (28ms)
console.log
global.$: function () {
return "I'm fake jquery";
}
at Object.<anonymous> (Whosebug/61727628/b.test.js:4:15)
console.log
global.$: function () {
return "I'm fake jquery";
}
at Object.<anonymous> (Whosebug/61727628/a.test.js:4:15)
console.log
global.moment: function () {
return "I'm fake moment";
}
at Object.<anonymous> (Whosebug/61727628/a.test.js:5:15)
console.log
global.moment: function () {
return "I'm fake moment";
}
at Object.<anonymous> (Whosebug/61727628/b.test.js:5:15)
PASS Whosebug/61727628/b.test.js
61727628
b
✓ should pass (28ms)
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 6.026s, estimated 16s