jestjs - Jest, TypeORM och TypeScript misslyckas med att köra tester
Jag använder TS med TypeORM, MySQL och Jest för att bygga en schemafabrik, men mina testsviter kan inte köras eftersom de ger ett syntaxfel angående import-satsen. Den huvudsakliga delen jag blir förvirrad över är det faktum att projektet bygger och hamnar i dist/-mappen, men att köra en testsvit som nedan ger ett fel. All hjälp skulle uppskattas eftersom jag inte riktigt vet vad problemet är.
Fel kastas
/Users/xxxx/Work/schemasproject/src/entity/HistoricalRental.ts:1
import { Entity } from 'typeorm';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:353:18)
at wrapSafe (node:internal/modules/cjs/loader:1039:15)
at Module._compile (node:internal/modules/cjs/loader:1073:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Module.require (node:internal/modules/cjs/loader:1013:19)
at require (node:internal/modules/cjs/helpers:93:18)
at /Users/xxxx/Work/schemasproject/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:39
at Array.map (<anonymous>)
index.test.ts
describe('Historical Sales Tests', () => {
beforeAll(async () => {
console.log('before');
});
describe('Creation', () => {
it('Should create the row', async () => {
console.log('here');
});
});
});
/src/entity/HistoricalRental.ts - där Property är en grundläggande klass med en primärnyckel och några kolumner
import { Entity } from 'typeorm';
import { Property } from '../abstract/Property';
@Entity()
export class HistoricalRental extends Property {}
package.json
{
"name": "schemas",
"version": "1.0.0",
"description": "",
"main": "src/index.ts",
"scripts": {
"build": "rm -rf dist && tsc",
"lint": "eslint './src/**/*.{ts,tsx}'",
"postpublish": "git clean -fd",
"prepublishOnly": "npm run build && cp -r ./dist/* . && rm -rf ./dist",
"pretest": "npm run lint",
"test": "jest --config=jest.config.js -i --forceExit",
"testOnly": "jest --config=jest.config.js -i --forceExit"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jest": "^26.0.24",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"eslint": "^7.31.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.4",
"jest": "^27.0.6",
"ts-jest": "^27.0.4",
"typescript": "^4.3.5"
},
"dependencies": {
"mysql": "^2.18.1",
"typeorm": "^0.2.34"
}
}
jest.config.js
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/src'],
globalSetup: './src/__tests__/environment/globalSetup.ts',
globalTeardown: './src/__tests__/environment/globalTeardown.ts',
clearMocks: true,
maxWorkers: 1,
preset: 'ts-jest',
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
},
testRegex: '(\\.|/)(test|spec)\\.ts?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
collectCoverage: true,
coverageReporters: ['text'],
};
tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "ES6",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": false,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"emitDeclarationOnly": true
},
"exclude": ["./src/__tests__", "./src/__mocks__", "./src/**/*.spec.ts", "./dist/"]
}
ormconfig.js
const { join } = require('path');
module.exports = {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'test',
password: 'test',
database: 'test',
synchronize: true,
logging: false,
entities: [join(__dirname, './**/*.entity{.ts,.js}')],
migrations: [
'src/migration/**/*.ts',
],
subscribers: [
'src/subscriber/**/*.ts',
],
cli: {
entitiesDir: 'src/entity',
migrationsDir: 'src/migration',
subscribersDir: 'src/subscriber',
},
};