Skip to content

Commit

Permalink
chore(repo): use swc in favor of ts-jest (#2225)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Aug 23, 2022
1 parent 8517b69 commit 70ab1d5
Show file tree
Hide file tree
Showing 18 changed files with 657 additions and 981 deletions.
12 changes: 5 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ commands:
parameters:
max-workers:
type: integer
default: 3
default: 2
steps:
- run: yarn pretest
- run:
Expand All @@ -117,7 +117,7 @@ commands:
type: string
max-workers:
type: integer
default: 3
default: 2
steps:
- compute-hashes
- restore_cache:
Expand Down Expand Up @@ -149,6 +149,7 @@ jobs:
lint:
docker:
- image: circleci/node:lts
resource_class: small
steps:
- checkout
- cached-dependencies
Expand All @@ -162,18 +163,15 @@ jobs:
type: string
docker:
- image: circleci/node:<< parameters.node-version >>
# we should be able to restore the default when the following issue is resolved
# - https://github.com/kulshekhar/ts-jest/issues/1967
# - https://github.com/facebook/jest/issues/10550
resource_class: medium+
resource_class: medium
steps:
- checkout
- install-and-build
- test-node
- test-harness:
os: linux
node-version: << parameters.node-version >>
max-workers: 3
max-workers: 2

test-browser:
docker:
Expand Down
26 changes: 15 additions & 11 deletions jest.config.js → jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
/*eslint-env node*/
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const path = require('path');
const { mapValues } = require('lodash');
const { compilerOptions } = require('./tsconfig.json');
import { pathsToModuleNameMapper } from 'ts-jest';
import * as path from 'path';
import * as fs from 'fs';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const { compilerOptions } = JSON.parse(fs.readFileSync('./tsconfig.json', 'utf8'));

const projectDefault = {
preset: 'ts-jest',
moduleNameMapper: {
...mapValues(pathsToModuleNameMapper(compilerOptions.paths), v => path.join(__dirname, v)),
...Object.fromEntries(
Object.entries(pathsToModuleNameMapper(compilerOptions.paths)).map(([k, v]) => [k, path.join(__dirname, v)]),
),
'^@stoplight/spectral\\-test\\-utils$': '<rootDir>/test-utils/node/index.ts',
},
testEnvironment: 'node',
globals: {
'ts-jest': {
useIsolatedModules: true,
},
transform: {
'^.+\\.ts$': ['@swc/jest'],
},
};

module.exports = {
export default {
projects: [
{
...projectDefault,
Expand Down Expand Up @@ -58,6 +60,7 @@ module.exports = {
name: '@stoplight/spectral-ruleset-bundler',
color: 'blueBright',
},
setupFilesAfterEnv: ['<rootDir>/packages/ruleset-bundler/jest.setup.mjs'],
testMatch: ['<rootDir>/packages/ruleset-bundler/src/**/__tests__/**/*.{test,spec}.ts'],
},
{
Expand Down Expand Up @@ -87,6 +90,7 @@ module.exports = {
name: '@stoplight/spectral-rulesets',
color: 'cyanBright',
},
setupFilesAfterEnv: ['<rootDir>/packages/rulesets/jest.setup.mjs'],
testMatch: ['<rootDir>/packages/rulesets/src/**/__tests__/**/*.{test,spec}.ts'],
},
{
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@swc/core": "^1.2.210",
"@swc/jest": "^0.2.21",
"@types/file-entry-cache": "^5.0.2",
"@types/jest": "^27.0.3",
"@types/jest": "^28.1.6",
"@types/jest-when": "^2.7.3",
"@types/karma": "^6.3.1",
"@types/lodash": "^4.14.176",
Expand All @@ -86,12 +86,13 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-prettier": "^4.0.0",
"expect": "^27.5.1",
"fast-glob": "^3.2.7",
"fetch-mock": "^9.11.0",
"file-entry-cache": "^6.0.1",
"husky": "^7.0.4",
"jest": "^27.4.3",
"jest-mock": "^27.4.2",
"jest": "^28.1.3",
"jest-mock": "^27.5.1",
"jest-when": "^3.4.2",
"karma": "^6.1.1",
"karma-chrome-launcher": "^3.1.0",
Expand All @@ -105,8 +106,8 @@
"prettier": "^2.4.1",
"semantic-release": "^18.0.1",
"semantic-release-monorepo": "^7.0.5",
"ts-jest": "^27.0.7",
"ts-node": "^10.4.0",
"ts-jest": "^28.0.5",
"ts-node": "^10.8.2",
"typescript": "^4.4.4"
},
"lint-staged": {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/replacer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dictionary } from '@stoplight/types';
import eval from 'simple-eval';
import _eval from 'simple-eval';

export type Transformer<V = Record<string, unknown>> = (this: V, ...args: unknown[]) => string;

Expand All @@ -24,7 +24,7 @@ export class Replacer<V extends Record<string, unknown>> {

if (shouldEvaluate) {
return String(
eval(identifier, {
_eval(identifier, {
...Object.entries(this.functions).reduce((fns, [name, fn]) => {
fns[name] = fn.bind(values);
return fns;
Expand Down
7 changes: 6 additions & 1 deletion packages/functions/src/__tests__/__helpers__/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
RulesetFunctionWithValidator,
RulesetValidationError,
} from '@stoplight/spectral-core';
import { isAggregateError } from '@stoplight/spectral-core/src/guards/isAggregateError';

import { isError } from 'lodash';

function isAggregateError(maybeAggregateError: unknown): maybeAggregateError is Error & { errors: unknown[] } {
return isError(maybeAggregateError) && maybeAggregateError.constructor.name === 'AggregateError';
}

export default async function <O = unknown>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 2 additions & 0 deletions packages/ruleset-bundler/jest.setup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*eslint-env jest*/
jest.mock('fs');
2 changes: 0 additions & 2 deletions packages/ruleset-bundler/src/__tests__/index.jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { virtualFs } from '../plugins/virtualFs';
import { runtime } from '../presets/runtime';
import { builtins } from '../plugins/builtins';

jest.mock('fs');

describe('Ruleset Bundler', () => {
let io: IO;

Expand Down
2 changes: 0 additions & 2 deletions packages/ruleset-bundler/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { runtime } from '../presets/runtime';
import { browser } from '../presets/browser';
import { virtualFs } from '../plugins/virtualFs';

jest.mock?.('fs');

describe('Ruleset Bundler', () => {
let io: IO;

Expand Down
2 changes: 0 additions & 2 deletions packages/ruleset-bundler/src/loader/__tests__/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { bundleAndLoadRuleset } from '../browser';
import { IO } from '../../types';
import { DiagnosticSeverity } from '@stoplight/types';

jest.mock?.('fs');

describe('bundleAndLoadRuleset browser', () => {
let io: IO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { bundleAndLoadRuleset } from '../node';
import { IO } from '../../types';
import { DiagnosticSeverity } from '@stoplight/types';

jest.unmock('fs');

describe('bundleAndLoadRuleset node', () => {
let io: IO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { serveAssets } from '@stoplight/spectral-test-utils';
import * as runtime from '@stoplight/spectral-runtime';
import * as functions from '@stoplight/spectral-functions';

jest.mock?.('fs');

import { BundleOptions, bundleRuleset } from '../../index';
import type { IO } from '../../types';
import { virtualFs } from '../virtualFs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as fs from 'fs';
import { serveAssets } from '@stoplight/spectral-test-utils';
import { fetch } from '@stoplight/spectral-runtime';

jest.mock?.('fs');

import { BundleOptions, bundleRuleset } from '../../index';
import type { IO } from '../../types';
import { virtualFs } from '../virtualFs';
Expand Down
2 changes: 0 additions & 2 deletions packages/ruleset-bundler/src/plugins/__tests__/url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as fs from 'fs';
import { serveAssets, mockResponses } from '@stoplight/spectral-test-utils';
import { fetch } from '@stoplight/spectral-runtime';

jest.mock?.('fs');

import { bundleRuleset } from '../../index';
import { virtualFs } from '../virtualFs';
import type { IO } from '../../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as fs from 'fs';
import { serveAssets } from '@stoplight/spectral-test-utils';
import { fetch } from '@stoplight/spectral-runtime';

jest.mock?.('fs');

import { bundleRuleset } from '../../index';
import { virtualFs } from '../virtualFs';
import type { IO } from '../../types';
Expand Down
2 changes: 2 additions & 0 deletions packages/rulesets/jest.setup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*eslint-env jest*/
jest.mock('fs');
2 changes: 0 additions & 2 deletions packages/rulesets/src/__tests__/__helpers__/tester.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
jest.mock?.('fs');

import { serveAssets } from '@stoplight/spectral-test-utils';
import { IRuleResult, Spectral, Document, RulesetDefinition } from '@stoplight/spectral-core';
import { httpAndFileResolver } from '@stoplight/spectral-ref-resolver';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { typedEnum } from '../typedEnum';
import { Document } from '@stoplight/spectral-core';
import { DocumentInventory } from '@stoplight/spectral-core/src/documentInventory';
import * as Parsers from '@stoplight/spectral-parsers';
import { Resolver } from '@stoplight/spectral-ref-resolver';

function runTypedEnum(targetVal: any) {
const doc = new Document(JSON.stringify(targetVal), Parsers.Json);

return typedEnum(targetVal, null, {
path: [],
document: doc,
documentInventory: new DocumentInventory(doc, new Resolver()),
documentInventory: {} as any,
rule: {} as any,
});
}
Expand Down
Loading

0 comments on commit 70ab1d5

Please sign in to comment.