Skip to content

Commit

Permalink
chore(deps-dev): bump Karma and related packages
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Aug 25, 2022
1 parent 12aa6c1 commit 60a56df
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 346 deletions.
27 changes: 21 additions & 6 deletions __karma__/jest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @ts-ignore
process.stdout = {};

import { Expect } from 'expect/build/types';
import * as JestMock from 'jest-mock';

Expand All @@ -23,13 +26,25 @@ expect.extend({
test.each = input => (name: string, fn: Function) => {
// very simple stub-like implementation needed by src/rulesets/oas/__tests__/valid-example.ts and src/rulesets/__tests__/validation.test.ts
for (const value of input) {
if (Array.isArray(value)) {
fn(...value);
} else {
fn(value);
}
it(name, () => {
if (Array.isArray(value) && fn.length !== 1) {
return fn(...value);
} else {
return fn(value);
}
});
}
};

// @ts-ignore
describe.each = test.each;
describe.each = input => (name: string, fn: Function) => {
for (const value of input) {
describe(name, () => {
if (Array.isArray(value) && fn.length !== 1) {
return fn(...value);
} else {
return fn(value);
}
});
}
};
2 changes: 1 addition & 1 deletion karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = (config: Config): void => {
},
},
acornOptions: {
ecmaVersion: 11,
ecmaVersion: 13,
},
transforms: [
require('karma-typescript-es6-transform')({
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@types/file-entry-cache": "^5.0.2",
"@types/jest": "^28.1.6",
"@types/jest-when": "^2.7.3",
"@types/karma": "^6.3.1",
"@types/karma": "^6.3.3",
"@types/lodash": "^4.14.176",
"@types/node": "^15.12.4",
"@types/node-fetch": "^2.5.12",
Expand All @@ -94,11 +94,11 @@
"jest": "^28.1.3",
"jest-mock": "^27.5.1",
"jest-when": "^3.4.2",
"karma": "^6.1.1",
"karma-chrome-launcher": "^3.1.0",
"karma-jasmine": "^3.3.1",
"karma-typescript": "^5.5.2",
"karma-typescript-es6-transform": "^5.5.2",
"karma": "^6.4.0",
"karma-chrome-launcher": "^3.1.1",
"karma-jasmine": "^5.1.0",
"karma-typescript": "^5.5.3",
"karma-typescript-es6-transform": "^5.5.3",
"lint-staged": "^11.2.6",
"memfs": "^3.3.0",
"node-powershell": "^4.0.0",
Expand Down
22 changes: 15 additions & 7 deletions packages/ruleset-migrator/scripts/generate-test-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@ fs.promises.readdir(cwd).then(async ls => {
for (const dirname of ls) {
if (dirname === '.cache') continue;
const dirpath = path.join(cwd, dirname);
const bundle = {};
bundled[dirname] = bundle;
promises.push(
fs.promises.readFile(path.join(dirpath, 'output.cjs'), 'utf8').then(assign(bundle, 'output.cjs')),
fs.promises.readFile(path.join(dirpath, 'output.mjs'), 'utf8').then(assign(bundle, 'output.mjs')),
fs.promises.readFile(path.join(dirpath, 'ruleset.yaml'), 'utf8').then(assign(bundle, 'ruleset')),
readdir(bundle, dirpath, 'assets').catch(() => {
fs.promises
.readFile(path.join(dirpath, 'output.cjs'), 'utf8')
.then(assign(bundled, path.join(dirname, 'output.cjs'))),
fs.promises
.readFile(path.join(dirpath, 'output.mjs'), 'utf8')
.then(assign(bundled, path.join(dirname, 'output.mjs'))),
fs.promises
.readFile(path.join(dirpath, 'ruleset.yaml'), 'utf8')
.then(assign(bundled, path.join(dirname, 'ruleset'))),
readdir(bundled, cwd, path.join(dirname, 'assets')).catch(() => {
// it may not exist
}),
);
}

await Promise.all(promises);
await fs.promises.writeFile(path.join(cwd, '.cache/index.json'), JSON.stringify(bundled, null, 2));
await fs.promises.writeFile(path.join(cwd, '.cache/index.json'), JSON.stringify(sortKeys(bundled), null, 2));
});

function sortKeys<T>(input: T): T {
return Object.fromEntries(Object.entries(input).sort(([a], [b]) => a.localeCompare(b))) as T;
}

function assign(bundled: Record<string, string>, name: string) {
return async (input: string): Promise<void> => {
bundled[name] = /\.[mc]js$/.test(name) ? prettier.format(input as string, { parser: 'babel' }) : (input as string);
Expand Down
64 changes: 27 additions & 37 deletions packages/ruleset-migrator/src/__tests__/ruleset.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fs } from 'memfs';
import { vol } from 'memfs';
import * as path from '@stoplight/path';
import * as prettier from 'prettier/standalone';
import * as parserBabel from 'prettier/parser-babel';
Expand All @@ -12,31 +12,21 @@ import fixtures from './__fixtures__/.cache/index.json';

const cwd = '/.tmp/spectral';

describe('migrator', () => {
beforeAll(async () => {
await fs.promises.mkdir(cwd, { recursive: true });
});
vol.fromJSON(fixtures, cwd);

afterAll(() => {
fs.rmdirSync(cwd, { recursive: true });
});
afterAll(() => {
vol.reset();
});

const scenarios = Object.keys(fixtures)
.filter(key => path.basename(key) === 'output.mjs')
.map(key => path.dirname(key));

describe.each<[string, Record<string, string>]>([...Object.entries(fixtures)])('%s', (name, entries) => {
describe('migrator', () => {
describe.each<string>(scenarios)('%s', name => {
const dir = path.join(cwd, name);
const ruleset = path.join(dir, 'ruleset');

beforeAll(async () => {
await fs.promises.mkdir(dir, { recursive: true });
for (const [name, content] of Object.entries(entries)) {
await fs.promises.mkdir(path.join(dir, path.dirname(name)), { recursive: true });
await fs.promises.writeFile(path.join(dir, name), content);
}
});

afterAll(() => {
fs.rmdirSync(dir, { recursive: true });
});

it.each<[format: 'commonjs' | 'esm', ext: string]>([
['commonjs', '.cjs'],
['esm', '.mjs'],
Expand All @@ -45,16 +35,16 @@ describe('migrator', () => {
prettier.format(
await migrateRuleset(ruleset, {
format,
fs: fs as any,
fs: vol as any,
}),
{ parser: 'babel', plugins: [parserBabel] },
),
).toEqual(await fs.promises.readFile(path.join(dir, `output${ext}`), 'utf8'));
).toEqual(await vol.promises.readFile(path.join(dir, `output${ext}`), 'utf8'));
});
});

it('should support subsequent migrations', async () => {
await fs.promises.writeFile(
await vol.promises.writeFile(
path.join(cwd, 'ruleset-migration-1.json'),
JSON.stringify({
extends: ['./ruleset-migration-2.json'],
Expand All @@ -64,7 +54,7 @@ describe('migrator', () => {
}),
);

await fs.promises.writeFile(
await vol.promises.writeFile(
path.join(cwd, 'ruleset-migration-2.json'),
JSON.stringify({
extends: 'spectral:oas',
Expand All @@ -85,7 +75,7 @@ describe('migrator', () => {
'module, require',
await migrateRuleset(path.join(cwd, 'ruleset-migration-1.json'), {
format: 'commonjs',
fs: fs as any,
fs: vol as any,
}),
)(_module, (id: string): unknown => {
switch (id) {
Expand All @@ -112,7 +102,7 @@ describe('migrator', () => {
// something is off with default module interop in Karma :man_shrugging:
const fetch = ((fetchMock as { default?: typeof import('fetch-mock') }).default ?? fetchMock).sandbox();

await fs.promises.writeFile(
await vol.promises.writeFile(
path.join(cwd, 'ruleset.json'),
JSON.stringify({
extends: ['https://spectral.stoplight.io/ruleset'],
Expand All @@ -138,7 +128,7 @@ describe('migrator', () => {
expect(
await migrateRuleset(path.join(cwd, 'ruleset.json'), {
format: 'esm',
fs: fs as any,
fs: vol as any,
fetch,
}),
).toEqual(`export default {
Expand All @@ -161,11 +151,11 @@ describe('migrator', () => {

describe('error handling', () => {
it('given unknown format, should throw', async () => {
await fs.promises.writeFile(path.join(cwd, 'unknown-format.json'), `{ "formats": ["json-schema-draft-2"] }`);
await vol.promises.writeFile(path.join(cwd, 'unknown-format.json'), `{ "formats": ["json-schema-draft-2"] }`);
await expect(
migrateRuleset(path.join(cwd, 'unknown-format.json'), {
format: 'esm',
fs: fs as any,
fs: vol as any,
}),
).rejects.toThrow('Invalid ruleset provided');
});
Expand All @@ -186,7 +176,7 @@ describe('migrator', () => {
},
},
});
await fs.promises.writeFile(
await vol.promises.writeFile(
path.join(cwd, 'custom-npm-provider.json'),
JSON.stringify({
extends: ['custom-npm-ruleset'],
Expand All @@ -204,7 +194,7 @@ describe('migrator', () => {
expect(
await migrateRuleset(path.join(cwd, 'custom-npm-provider.json'), {
format: 'esm',
fs: fs as any,
fs: vol as any,
npmRegistry: 'https://unpkg.com/',
}),
).toEqual(`import {oas2} from "https://unpkg.com/@stoplight/spectral-formats";
Expand Down Expand Up @@ -235,7 +225,7 @@ export default {
});

it('should not apply to custom functions', async () => {
await fs.promises.writeFile(
await vol.promises.writeFile(
path.join(cwd, 'custom-npm-provider-custom-functions.json'),
JSON.stringify({
functions: ['customFunction'],
Expand All @@ -252,7 +242,7 @@ export default {
expect(
await migrateRuleset(path.join(cwd, 'custom-npm-provider-custom-functions.json'), {
format: 'esm',
fs: fs as any,
fs: vol as any,
npmRegistry: 'https://unpkg.com/',
}),
).toEqual(`import customFunction from "/.tmp/spectral/functions/customFunction.js";
Expand All @@ -270,7 +260,7 @@ export default {
});

it('should not apply to custom functions living outside of cwd', async () => {
await fs.promises.writeFile(
await vol.promises.writeFile(
path.join(cwd, 'custom-npm-provider-custom-functions.json'),
JSON.stringify({
functionsDir: '../fns',
Expand All @@ -289,7 +279,7 @@ export default {
expect(
await migrateRuleset(path.join(cwd, 'custom-npm-provider-custom-functions.json'), {
format: 'esm',
fs: fs as any,
fs: vol as any,
npmRegistry: 'https://unpkg.com/',
}),
).toEqual(`import customFunction from "/.tmp/fns/customFunction.js";
Expand All @@ -313,7 +303,7 @@ export default {
// @ts-expect-error: npmRegistry not accepted
{
format: 'commonjs',
fs: fs as any,
fs: vol as any,
npmRegistry: 'https://unpkg.com/',
},
),
Expand Down
Loading

0 comments on commit 60a56df

Please sign in to comment.