模块模式变量在测试中返回未定义?
Module pattern variable returning undefined in test?
我有下面的代码 returns 某些数据取决于 NODE_ENV:
config.js
export const Config = (() => {
let data;
switch (process.env.NODE_ENV) {
case 'development':
data = '123';
break;
case 'production':
data = '456'
break;
default:
break;
}
return {
data
};
})();
当我设置 NODE_ENV 时,这在我的组件中运行良好。但是在我的测试中,结果我一直未定义。
config.test.js
describe('Config', () => {
test('returns correct data if NODE_ENV is development', () => {
process.env = { ...process.env, NODE_ENV: 'development' };
expect(Config.data).toBe('123'); // returns undefined, expected '123'
});
test('returns correct data if NODE_ENV is production', () => {
process.env = { ...process.env, NODE_ENV: 'production' };
expect(Config.data).toBe('456'); // returns undefined, expected '456'
});
});
同样,当我启动它时,Config.data
在我的 React 组件中工作正常,但我想我需要以某种方式初始化它才能在我的测试中工作?如有任何建议,我们将不胜感激!
首先,您需要确保在设置process.env
后导入了config
模块。所以你需要使用 const { Config } = require('./config')
而不是 import { Config } from './config';
因为 imports are hoisted,并且当 IIFE 执行时,process.env
没有准备好。
另一个注释是模块caching。
Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo')
will get exactly the same object returned, if it would resolve to the same file.
Provided require.cache
is not modified, multiple calls to require('foo')
will not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.
你的配置模块中有一个IIFE,当你require('./config')
多次时它只执行一次。 IIFE 中 process.env
的值也被缓存了。所以,你需要使用jest.resetModules()清除模块缓存。
例如
config.js
:
export const Config = (() => {
let data;
console.log(process.env.NODE_ENV);
switch (process.env.NODE_ENV) {
case 'development':
data = '123';
break;
case 'production':
data = '456';
break;
default:
break;
}
return { data };
})();
config.test.js
:
describe('Config', () => {
let Config;
beforeEach(() => {
jest.resetModules();
});
test('returns correct data if NODE_ENV is development', () => {
process.env = { ...process.env, NODE_ENV: 'development' };
Config = require('./config').Config;
expect(Config.data).toBe('123');
});
test('returns correct data if NODE_ENV is production', () => {
process.env = { ...process.env, NODE_ENV: 'production' };
Config = require('./config').Config;
expect(Config.data).toBe('456');
});
});
测试结果:
PASS Whosebug/71733750/config.test.ts
Config
✓ returns correct data if NODE_ENV is development (15 ms)
✓ returns correct data if NODE_ENV is production (2 ms)
console.log
development
at Whosebug/71733750/config.ts:29:11
console.log
production
at Whosebug/71733750/config.ts:29:11
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
config.ts | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.354 s
您可以尝试删除 jest.resetModules()
以检查日志。
我有下面的代码 returns 某些数据取决于 NODE_ENV:
config.js
export const Config = (() => {
let data;
switch (process.env.NODE_ENV) {
case 'development':
data = '123';
break;
case 'production':
data = '456'
break;
default:
break;
}
return {
data
};
})();
当我设置 NODE_ENV 时,这在我的组件中运行良好。但是在我的测试中,结果我一直未定义。
config.test.js
describe('Config', () => {
test('returns correct data if NODE_ENV is development', () => {
process.env = { ...process.env, NODE_ENV: 'development' };
expect(Config.data).toBe('123'); // returns undefined, expected '123'
});
test('returns correct data if NODE_ENV is production', () => {
process.env = { ...process.env, NODE_ENV: 'production' };
expect(Config.data).toBe('456'); // returns undefined, expected '456'
});
});
同样,当我启动它时,Config.data
在我的 React 组件中工作正常,但我想我需要以某种方式初始化它才能在我的测试中工作?如有任何建议,我们将不胜感激!
首先,您需要确保在设置process.env
后导入了config
模块。所以你需要使用 const { Config } = require('./config')
而不是 import { Config } from './config';
因为 imports are hoisted,并且当 IIFE 执行时,process.env
没有准备好。
另一个注释是模块caching。
Modules are cached after the first time they are loaded. This means (among other things) that every call to
require('foo')
will get exactly the same object returned, if it would resolve to the same file.
Provided
require.cache
is not modified, multiple calls torequire('foo')
will not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.
你的配置模块中有一个IIFE,当你require('./config')
多次时它只执行一次。 IIFE 中 process.env
的值也被缓存了。所以,你需要使用jest.resetModules()清除模块缓存。
例如
config.js
:
export const Config = (() => {
let data;
console.log(process.env.NODE_ENV);
switch (process.env.NODE_ENV) {
case 'development':
data = '123';
break;
case 'production':
data = '456';
break;
default:
break;
}
return { data };
})();
config.test.js
:
describe('Config', () => {
let Config;
beforeEach(() => {
jest.resetModules();
});
test('returns correct data if NODE_ENV is development', () => {
process.env = { ...process.env, NODE_ENV: 'development' };
Config = require('./config').Config;
expect(Config.data).toBe('123');
});
test('returns correct data if NODE_ENV is production', () => {
process.env = { ...process.env, NODE_ENV: 'production' };
Config = require('./config').Config;
expect(Config.data).toBe('456');
});
});
测试结果:
PASS Whosebug/71733750/config.test.ts
Config
✓ returns correct data if NODE_ENV is development (15 ms)
✓ returns correct data if NODE_ENV is production (2 ms)
console.log
development
at Whosebug/71733750/config.ts:29:11
console.log
production
at Whosebug/71733750/config.ts:29:11
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
config.ts | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.354 s
您可以尝试删除 jest.resetModules()
以检查日志。