Following is my node js test file code , This is a unit test case, and its failing. Please find the code and error message in detail below.
jest.unmock('./utils.js');
describe('test', () => {
it('test', async (done) => {
await expect(getAPISecretKey('testKey')).rejects.toEqual('RestError: AKV10000: Request is missing a Bearer or PoP token.');
});
});
It's failing with the below error
FAIL src/utils.test.js
● test › test
expect(received).rejects.toEqual(expected) // deep equality
Expected: ["RestError: AKV10000: Request is missing a Bearer or PoP token."]
Received: [RestError: AKV10000: Request is missing a Bearer or PoP token.]
6 | it('test', async (done) => {
> 7 | await expect(getAPISecretKey('testKey')).rejects.toEqual(error);
| ^
8 | });
9 | });
10 |
at Object.toEqual (node_modules/expect/build/index.js:241:20)
at Object.<anonymous> (src/utils.test.js:7:58)
I tried with following way but still, it didn't work.
jest.unmock('./utils.js');
describe('test', () => {
var error = ['RestError: AKV10000: Request is missing a Bearer or PoP token.'];
it('test', async (done) => {
await expect(getAPISecretKey('testKey')).rejects.toEqual(new Error('Request is missing a Bearer or PoP token.'));
});
});
code of getAPISecretKey
async function getAPISecretKey(secretNameStr) {
let credentials = getKeyVaultCredentials();
let keyVaultClient = new SecretClient(KEY_VAULT_URL, credentials);
let secret = await keyVaultClient.getSecret(secretNameStr);
return secret.value;
}
Tried following way and giving an error as follows
jest.unmock('./utils.js');
describe('test', () => {
//toEqual('RestError: AKV10000: Request is missing a Bearer or PoP token.');
it('test', async (done) => {
await expect(getAPISecretKey('testKey')).rejects.toThrow('AKV10000: Request is missing a Bearer or PoP token.');
});
});
Error:
FAIL src/utils.test.js (14.891 s)
● test › test
: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:
4 | describe('test', () => {
5 | //toEqual('RestError: AKV10000: Request is missing a Bearer or PoP token.');
> 6 | it('test', async (done) => {
| ^
7 | await expect(getAPISecretKey('testKey')).rejects.toThrow('AKV10000: Request is missing a Bearer or PoP token.');
8 |
at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
at Suite.<anonymous> (src/utils.test.js:6:6)
at Object.<anonymous> (src/utils.test.js:4:1)
CodePudding user response:
I am able to resolve the issue by removing "done" from the async method arguments as follows.
jest.unmock('./utils.js');
//jest.setTimeout(10000);
describe('test', () => {
it('test', async () => {
await expect(getAPISecretKey('testKey')).rejects.toThrow('AKV10000: Request is missing a Bearer or PoP token.');
});
});