I am trying to set up jest and run a simple test (sum from jestjs.io).
sum.ts file:
function sum(a: any, b: any):any {
return a + b;
}
module.exports = sum;
sum.test.ts file:
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
In the console I am getting:
yarn test
yarn run v1.22.17
warning ../../../package.json: No license field
$ jest
FAIL src/sum.test.ts
● Test suite failed to run
src/sum.test.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'sum'.
1 const sum = require('./sum');
~~~
src/sum.ts:1:10
1 function sum(a: any, b: any) {
~~~
'sum' was also declared here.
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.87 s
Ran all test suites.
error Command failed with exit code 1.
How to resolve this?
If change import in the file 'sum.test.ts' like this,
import { sum } from './sum'
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Console:
yarn test
yarn run v1.22.17
warning ../../../package.json: No license field
$ jest
FAIL src/sum.test.ts
● Test suite failed to run
src/sum.test.ts:1:21 - error TS2306: File '/Users/pasmo/Code/sandbox/.../src/sum.ts' is not a module.
1 import { sum } from './sum'
~~~~~~~
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.812 s
Ran all test suites.
error Command failed with exit code 1.
................