feat: Use vitest instead of jest for ESM compatibility

This commit is contained in:
Faruk AYDIN
2024-01-12 14:07:59 +01:00
parent 911159a14f
commit af9ceac0b4
19 changed files with 655 additions and 66 deletions

View File

@@ -1,9 +0,0 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFilesAfterEnv: ['./test/setup/global-hooks.ts'],
globalTeardown: './test/setup/global-teardown.ts',
collectCoverage: true,
collectCoverageFrom: ['src/graphql/queries/*.ts'],
};

View File

@@ -9,8 +9,8 @@
"worker": "nodemon --watch 'src/**/*.js' --exec 'node' src/worker.js",
"start": "node src/server.js",
"start:worker": "node src/worker.js",
"pretest": "APP_ENV=test ts-node ./test/setup/prepare-test-env.ts",
"test": "APP_ENV=test jest --verbose",
"pretest": "APP_ENV=test node ./test/setup/prepare-test-env.js",
"test": "APP_ENV=test vitest run",
"lint": "eslint . --ignore-path ../../.eslintignore",
"db:create": "ts-node ./bin/database/create.js",
"db:seed:user": "ts-node ./bin/database/seed-user.js",
@@ -140,7 +140,8 @@
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
"ts-node": "^10.2.1",
"ts-node-dev": "^1.1.8"
"ts-node-dev": "^1.1.8",
"vitest": "^1.1.3"
},
"publishConfig": {
"access": "public"

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import * as license from '../../helpers/license.ee';
@@ -22,10 +22,10 @@ describe('graphQL getAutomatischInfo query', () => {
describe('and without valid license', () => {
beforeEach(async () => {
jest.spyOn(license, 'getLicense').mockResolvedValue(false);
vi.spyOn(license, 'getLicense').mockResolvedValue(false);
jest.replaceProperty(appConfig, 'isCloud', false);
jest.replaceProperty(appConfig, 'isMation', false);
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false);
vi.spyOn(appConfig, 'isMation', 'get').mockReturnValue(false);
});
it('should return empty license data', async () => {
@@ -62,12 +62,12 @@ describe('graphQL getAutomatischInfo query', () => {
verified: true,
};
jest.spyOn(license, 'getLicense').mockResolvedValue(mockedLicense);
vi.spyOn(license, 'getLicense').mockResolvedValue(mockedLicense);
});
describe('and with cloud flag enabled', () => {
beforeEach(async () => {
jest.replaceProperty(appConfig, 'isCloud', true);
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(true);
});
it('should return all license data', async () => {
@@ -97,7 +97,7 @@ describe('graphQL getAutomatischInfo query', () => {
describe('and with cloud flag disabled', () => {
beforeEach(async () => {
jest.replaceProperty(appConfig, 'isCloud', false);
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false);
});
it('should return all license data', async () => {
@@ -127,8 +127,8 @@ describe('graphQL getAutomatischInfo query', () => {
describe('and with mation flag enabled', () => {
beforeEach(async () => {
jest.replaceProperty(appConfig, 'isCloud', false);
jest.replaceProperty(appConfig, 'isMation', true);
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false);
vi.spyOn(appConfig, 'isMation', 'get').mockReturnValue(true);
});
it('should return all license data', async () => {
@@ -158,8 +158,8 @@ describe('graphQL getAutomatischInfo query', () => {
describe('and with mation flag disabled', () => {
beforeEach(async () => {
jest.replaceProperty(appConfig, 'isCloud', false);
jest.replaceProperty(appConfig, 'isMation', false);
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false);
vi.spyOn(appConfig, 'isMation', 'get').mockReturnValue(false);
});
it('should return all license data', async () => {

View File

@@ -1,14 +1,11 @@
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import { createConfig } from '../../../test/factories/config';
import { IConfig } from '@automatisch/types';
import * as license from '../../helpers/license.ee';
describe('graphQL getConfig query', () => {
let configOne: IConfig,
configTwo: IConfig,
configThree: IConfig,
query: string;
let configOne, configTwo, configThree, query;
beforeEach(async () => {
configOne = await createConfig({ key: 'configOne' });
@@ -24,7 +21,7 @@ describe('graphQL getConfig query', () => {
describe('and without valid license', () => {
beforeEach(async () => {
jest.spyOn(license, 'hasValidLicense').mockResolvedValue(false);
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(false);
});
describe('and correct permissions', () => {
@@ -43,7 +40,7 @@ describe('graphQL getConfig query', () => {
describe('and with valid license', () => {
beforeEach(async () => {
jest.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
});
describe('and without providing specific keys', () => {

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import appConfig from '../../config/app';

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import appConfig from '../../config/app';

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
@@ -94,7 +94,7 @@ describe('graphQL getRole query', () => {
describe('with authenticated user', () => {
describe('and with valid license', () => {
beforeEach(async () => {
jest.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
});
describe('and without permissions', () => {
@@ -162,7 +162,7 @@ describe('graphQL getRole query', () => {
describe('and without valid license', () => {
beforeEach(async () => {
jest.spyOn(license, 'hasValidLicense').mockResolvedValue(false);
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(false);
});
describe('and correct permissions', () => {

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
@@ -73,7 +73,7 @@ describe('graphQL getRoles query', () => {
describe('with authenticated user', () => {
describe('and with valid license', () => {
beforeEach(async () => {
jest.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
});
describe('and without permissions', () => {
@@ -132,7 +132,7 @@ describe('graphQL getRoles query', () => {
describe('and without valid license', () => {
beforeEach(async () => {
jest.spyOn(license, 'hasValidLicense').mockResolvedValue(false);
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(false);
});
describe('and correct permissions', () => {

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { vi, describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import User from '../../models/user';
@@ -41,9 +41,9 @@ describe('graphQL getTrialStatus query', () => {
userToken = createAuthTokenByUserId(user.id);
});
describe('and with cloud flag disabled', () => {
describe.only('and with cloud flag disabled', () => {
beforeEach(async () => {
jest.replaceProperty(appConfig, 'isCloud', false);
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(false);
});
it('should return null', async () => {
@@ -63,15 +63,15 @@ describe('graphQL getTrialStatus query', () => {
describe('and with cloud flag enabled', () => {
beforeEach(async () => {
jest.replaceProperty(appConfig, 'isCloud', true);
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(true);
});
describe('and not in trial and has active subscription', () => {
beforeEach(async () => {
jest.spyOn(User.prototype, 'inTrial').mockResolvedValue(false);
jest
.spyOn(User.prototype, 'hasActiveSubscription')
.mockResolvedValue(true);
vi.spyOn(User.prototype, 'inTrial').mockResolvedValue(false);
vi.spyOn(User.prototype, 'hasActiveSubscription').mockResolvedValue(
true
);
});
it('should return null', async () => {
@@ -91,7 +91,7 @@ describe('graphQL getTrialStatus query', () => {
describe('and in trial period', () => {
beforeEach(async () => {
jest.spyOn(User.prototype, 'inTrial').mockResolvedValue(true);
vi.spyOn(User.prototype, 'inTrial').mockResolvedValue(true);
});
it('should return null', async () => {

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../app';
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';

View File

@@ -1,4 +1,4 @@
// @ts-nocheck
import { describe, it, expect } from 'vitest';
import request from 'supertest';
import app from '../../app';
import appConfig from '../../config/app';

View File

@@ -1,6 +1,8 @@
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const testEnvFile = path.resolve(__dirname, '../../.env.test');
if (!fs.existsSync(testEnvFile)) {

View File

@@ -1,6 +1,6 @@
import { Model } from 'objection';
import { client as knex } from '../../src/config/database';
import logger from '../../src/helpers/logger';
import { client as knex } from '../../src/config/database.js';
import logger from '../../src/helpers/logger.js';
global.beforeAll(async () => {
global.knex = null;
@@ -22,8 +22,8 @@ global.afterEach(async () => {
await global.knex.rollback();
Model.knex(knex);
jest.restoreAllMocks();
jest.clearAllMocks();
// jest.restoreAllMocks();
// jest.clearAllMocks();
});
global.afterAll(async () => {

View File

@@ -1,5 +0,0 @@
const exitProcess = (): void => {
process.exit(0);
};
export default exitProcess;

View File

@@ -1,8 +1,8 @@
import './check-env-file';
import { createDatabaseAndUser } from '../../bin/database/utils';
import { client as knex } from '../../src/config/database';
import logger from '../../src/helpers/logger';
import appConfig from '../../src/config/app';
import './check-env-file.js';
import { createDatabaseAndUser } from '../../bin/database/utils.js';
import { client as knex } from '../../src/config/database.js';
import logger from '../../src/helpers/logger.js';
import appConfig from '../../src/config/app.js';
const createAndMigrateDatabase = async () => {
if (!appConfig.CI) {

View File

@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'node',
setupFiles: ['./test/setup/global-hooks.js'],
globals: true,
},
});