I am trying to use jest to start unit testing and I came across this error like this one.
> npm run unit
> [email protected] unit
> NODE_OPTIONS=--experimental-vm-modules jest --config jest.config.ts
FAIL tests/unit/get.test.ts
● Test suite failed to run
ReferenceError: exports is not defined
1 | import { describe, expect, it } from '@jest/globals';
> 2 | import { APIGatewayProxyEvent, Context } from 'aws-lambda';
| ^
3 | import { lambdaHandler } from '../../app/app';
4 |
5 | describe('Unit test for syncAppInfoToAamDb', () => {
at tests/unit/get.test.ts:2:23
Here is my code.
import { describe, expect, it } from '@jest/globals';
import { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { lambdaHandler } from '../../app/app';
describe('Unit test for test', () => {
it('test', async () => {
....
const res = lambdaHandler(event, context);
expect(res).toEqual('test');
});
});
Earlier I asked here about Jest encountered an unexpected token
and modified jest.config.ts
Jest encountered an unexpected token For Typescrip
// jest.config.ts
export default {
preset: 'ts-jest/presets/default-esm',
transform: {
'^.+\\.ts?$': 'ts-jest',
},
clearMocks: true,
collectCoverage: true,
coverageDirectory: 'coverage',
coverageProvider: 'v8',
testMatch: ['**/tests/unit/*.test.ts'],
testPathIgnorePatterns: ['cf/.aws-sam/build'],
moduleNameMapper: {
'^/opt/nodejs/dist/db(.*)$': '<rootDir>/../../nodeLayer/db/dist/db$1',
},
};
Since jest said that it supports the commonjs format by default, we decided to add preset: 'ts-jest/presets/default-esm'
.
What is the cause of this problem? Please tell me if there is a problem with another file. I will include the descriptions of the other files.
npm install aws-lambda
? – Riser