如何用值对 javascript 对象进行单元测试?
How to unit test a javascript object with value?
我有一个 js 文件,我在其中读取一些值并将结果加载到一个变量中。我得到的好处是我不必每次需要时都从文件中读取。我可以简单地导入对象并使用它。
但是我不知道如何为它编写单元测试。
const fs = require('fs');
const read_file = (path) => {
try {
const data = fs.readFileSync(path, 'utf8');
return data;
} catch (err) {
console.error('Error in read_file', err);
throw err;
}
};
const getSecret = secretName => {
try {
return read_file(`/etc/secrets/${secretName}.txt`);
} catch (err){
throw err;
}
};
const secretConfig = {
kafka_keystore_password: getSecret('kafka_keystore_password')
};
module.exports = secretConfig;
我用jest写测试用例
我试过类似的方法,但对象仍然解析为未定义。
const secretConfig = require('./secret');
const fs = require('fs');
jest.mock('fs');
fs.readFileSync.mockReturnValue('randomPrivateKey');
describe('secret read files', () => {
it('should read secret from file', async () => {
const secretMessage = secretConfig.kafka_keystore_password;
expect(secretMessage).toEqual('randomPrivateKey');
})
})
secret read files › should read secret from file
expect(received).toEqual(expected) // deep equality
Expected: "randomPrivateKey"
Received: undefined
18 | const secretMessage = secretConfig.kafka_keystore_password;
19 | //expect(fs.readFileSync).toHaveBeenCalled();
> 20 | expect(secretMessage).toEqual('randomPrivateKey');
你的测试是正确的。问题是您需要在 require
之前模拟 ./secret
模块,因为 getSecret
方法将在需要模块时立即执行。此时,fs.readFileSync
还没有被赋予模拟值。
const fs = require('fs');
fs.readFileSync.mockReturnValue('randomPrivateKey');
const secretConfig = require('./secret');
jest.mock('fs');
describe('secret read files', () => {
it('should read secret from file', async () => {
const secretMessage = secretConfig.kafka_keystore_password;
expect(secretMessage).toEqual('randomPrivateKey');
});
});
测试结果:
> jest "-o" "--coverage"
PASS examples/69283738/secret.test.js (7.265 s)
secret read files
✓ should read secret from file (2 ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 76.92 | 100 | 100 | 76.92 |
secret.js | 76.92 | 100 | 100 | 76.92 | 8-9,17
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 7.794 s
我有一个 js 文件,我在其中读取一些值并将结果加载到一个变量中。我得到的好处是我不必每次需要时都从文件中读取。我可以简单地导入对象并使用它。
但是我不知道如何为它编写单元测试。
const fs = require('fs');
const read_file = (path) => {
try {
const data = fs.readFileSync(path, 'utf8');
return data;
} catch (err) {
console.error('Error in read_file', err);
throw err;
}
};
const getSecret = secretName => {
try {
return read_file(`/etc/secrets/${secretName}.txt`);
} catch (err){
throw err;
}
};
const secretConfig = {
kafka_keystore_password: getSecret('kafka_keystore_password')
};
module.exports = secretConfig;
我用jest写测试用例
我试过类似的方法,但对象仍然解析为未定义。
const secretConfig = require('./secret');
const fs = require('fs');
jest.mock('fs');
fs.readFileSync.mockReturnValue('randomPrivateKey');
describe('secret read files', () => {
it('should read secret from file', async () => {
const secretMessage = secretConfig.kafka_keystore_password;
expect(secretMessage).toEqual('randomPrivateKey');
})
})
secret read files › should read secret from file
expect(received).toEqual(expected) // deep equality Expected: "randomPrivateKey" Received: undefined 18 | const secretMessage = secretConfig.kafka_keystore_password; 19 | //expect(fs.readFileSync).toHaveBeenCalled(); > 20 | expect(secretMessage).toEqual('randomPrivateKey');
你的测试是正确的。问题是您需要在 require
之前模拟 ./secret
模块,因为 getSecret
方法将在需要模块时立即执行。此时,fs.readFileSync
还没有被赋予模拟值。
const fs = require('fs');
fs.readFileSync.mockReturnValue('randomPrivateKey');
const secretConfig = require('./secret');
jest.mock('fs');
describe('secret read files', () => {
it('should read secret from file', async () => {
const secretMessage = secretConfig.kafka_keystore_password;
expect(secretMessage).toEqual('randomPrivateKey');
});
});
测试结果:
> jest "-o" "--coverage"
PASS examples/69283738/secret.test.js (7.265 s)
secret read files
✓ should read secret from file (2 ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 76.92 | 100 | 100 | 76.92 |
secret.js | 76.92 | 100 | 100 | 76.92 | 8-9,17
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 7.794 s