Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
chore: moving tests over to jest (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Apr 3, 2023
1 parent deb271a commit a0ea00f
Show file tree
Hide file tree
Showing 10 changed files with 6,465 additions and 4,409 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.nyc_output/
coverage/
dist/
13 changes: 0 additions & 13 deletions .mocharc.json

This file was deleted.

2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.nyc_output/
coverage/
dist/
14 changes: 14 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
coveragePathIgnorePatterns: ['/dist', '/node_modules'],
modulePaths: ['<rootDir>'],
roots: ['<rootDir>'],
testRegex: '(/tes/.*|(\\.|/)(test|spec))\\.(js?|ts?)$',
transform: {
'^.+\\.[tj]s$': [
'ts-jest',
{
tsconfig: 'test/tsconfig.json',
},
],
},
};
10,637 changes: 6,355 additions & 4,282 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"pretest": "npm run lint",
"prettier": "prettier --list-different --write \"./**/**.{js,ts}\"",
"release": "npx conventional-changelog-cli -i CHANGELOG.md -s && git add CHANGELOG.md",
"test": "nyc mocha \"test/**/*.test.ts\""
"test": "jest --coverage"
},
"peerDependencies": {
"oas": "^20.0.0"
Expand All @@ -32,16 +32,13 @@
"@commitlint/config-conventional": "^17.4.2",
"@readme/eslint-config": "^10.5.0",
"@readme/oas-examples": "^5.7.1",
"@types/chai": "^4.3.1",
"@types/mocha": "^10.0.0",
"chai": "^4.3.6",
"@types/jest": "^29.5.0",
"eslint": "^8.33.0",
"eslint-plugin-mocha": "^10.0.4",
"husky": "^8.0.3",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"jest": "^29.5.0",
"prettier": "^2.8.3",
"typescript": "^4.9.5"
"ts-jest": "^29.1.0",
"typescript": "^5.0.3"
},
"prettier": "@readme/eslint-config/prettier",
"commitlint": {
Expand Down
2 changes: 1 addition & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "@readme/eslint-config/testing-mocha"
"extends": "@readme/eslint-config/testing"
}
5 changes: 0 additions & 5 deletions test/helpers/init.js

This file was deleted.

185 changes: 89 additions & 96 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
/* eslint-disable mocha/no-setup-in-describe */
import petstore from '@readme/oas-examples/3.0/json/petstore.json';
import { expect } from 'chai';
import Oas from 'oas';

import * as extensions from '../src';

describe('oas-extensions', function () {
[
'CODE_SAMPLES',
'EXPLORER_ENABLED',
'HEADERS',
'METRICS_ENABLED',
'PROXY_ENABLED',
'SAMPLES_ENABLED',
'SAMPLES_LANGUAGES',
'SEND_DEFAULTS',
'SIMPLE_MODE',
].forEach(extension => {
it(`\`${extension}\` extension should have a default value`, function () {
expect(extensions.defaults).to.have.property(extensions[extension]);
});
describe('oas-extensions', () => {
it.each([
['CODE_SAMPLES'],
['EXPLORER_ENABLED'],
['HEADERS'],
['METRICS_ENABLED'],
['PROXY_ENABLED'],
['SAMPLES_ENABLED'],
['SAMPLES_LANGUAGES'],
['SEND_DEFAULTS'],
['SIMPLE_MODE'],
])('%s should have a default value', extension => {
expect(extensions.defaults).toHaveProperty(extensions[extension]);
});

describe('#getExtension', function () {
it("should not throw an exception if `Oas` doesn't have an API definition", function () {
describe('#getExtension', () => {
it("should not throw an exception if `Oas` doesn't have an API definition", () => {
const oas = Oas.init(undefined);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).to.have.length(7);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).toHaveLength(7);
});

it("should not throw an exception if `Operation` doesn't have an API definition", function () {
it("should not throw an exception if `Operation` doesn't have an API definition", () => {
const oas = Oas.init(undefined);
const operation = oas.operation('/pet', 'post');
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas, operation)).to.have.length(7);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas, operation)).toHaveLength(7);
});

describe('oas-level extensions', function () {
it('should use the default extension value if the extension is not present', function () {
describe('oas-level extensions', () => {
it('should use the default extension value if the extension is not present', () => {
const oas = Oas.init(petstore);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).to.deep.equal([
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).toStrictEqual([
'shell',
'node',
'ruby',
Expand All @@ -48,32 +44,32 @@ describe('oas-extensions', function () {
]);
});

it('should locate an extensions under `x-readme`', function () {
it('should locate an extensions under `x-readme`', () => {
const oas = Oas.init({
...petstore,
'x-readme': {
[extensions.SAMPLES_LANGUAGES]: ['swift'],
},
});

expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).to.deep.equal(['swift']);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).toStrictEqual(['swift']);
});

it('should locate an extensions listed at the root', function () {
it('should locate an extensions listed at the root', () => {
const oas = Oas.init({ ...petstore, [`x-${extensions.EXPLORER_ENABLED}`]: false });
expect(extensions.getExtension(extensions.EXPLORER_ENABLED, oas)).to.be.false;
expect(extensions.getExtension(extensions.EXPLORER_ENABLED, oas)).toBe(false);
});

it('should not throw if `x-readme` is not an object', function () {
it('should not throw if `x-readme` is not an object', () => {
const oas = Oas.init({
...petstore,
'x-readme': true,
});

expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).to.have.length(7);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).toHaveLength(7);
});

it('should not pick up the `code-samples` extension', function () {
it('should not pick up the `code-samples` extension', () => {
const oas = Oas.init({
...petstore,
'x-readme': {
Expand All @@ -87,21 +83,21 @@ describe('oas-extensions', function () {
},
});

expect(extensions.getExtension(extensions.CODE_SAMPLES, oas)).to.be.undefined;
expect(extensions.getExtension(extensions.CODE_SAMPLES, oas)).toBeUndefined();
});
});

describe('operation-level', function () {
describe('operation-level', () => {
let oas;

beforeEach(function () {
beforeEach(() => {
oas = Oas.init(petstore);
});

it('should use the default extension value if the extension is not present', function () {
it('should use the default extension value if the extension is not present', () => {
const operation = oas.operation('/pet', 'post');

expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas, operation)).to.deep.equal([
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas, operation)).toStrictEqual([
'shell',
'node',
'ruby',
Expand All @@ -112,47 +108,47 @@ describe('oas-extensions', function () {
]);
});

it('should locate an extensions under `x-readme`', function () {
it('should locate an extensions under `x-readme`', () => {
const operation = oas.operation('/pet', 'post');
operation.schema['x-readme'] = {
[extensions.SAMPLES_LANGUAGES]: ['swift'],
};

expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas, operation)).to.deep.equal(['swift']);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas, operation)).toStrictEqual(['swift']);
});

it('should locate an extensions listed at the root', function () {
it('should locate an extensions listed at the root', () => {
const operation = oas.operation('/pet', 'post');
operation.schema[`x-${extensions.EXPLORER_ENABLED}`] = false;

expect(extensions.getExtension(extensions.EXPLORER_ENABLED, oas, operation)).to.be.false;
expect(extensions.getExtension(extensions.EXPLORER_ENABLED, oas, operation)).toBe(false);
});

it('should not throw if `x-readme` is not an object', function () {
it('should not throw if `x-readme` is not an object', () => {
const operation = oas.operation('/pet', 'post');
operation.schema['x-readme'] = true;

expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).to.have.length(7);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).toHaveLength(7);
});
});
});

describe('#isExtensionValid()', function () {
it('should validate that `x-readme` is an object', function () {
describe('#isExtensionValid()', () => {
it('should validate that `x-readme` is an object', () => {
expect(() => {
extensions.validateExtension(extensions.EXPLORER_ENABLED, Oas.init({ 'x-readme': [] }));
}).to.throw(/must be of type "Object"/);
}).toThrow(/must be of type "Object"/);

expect(() => {
extensions.validateExtension(extensions.EXPLORER_ENABLED, Oas.init({ 'x-readme': false }));
}).to.throw(/must be of type "Object"/);
}).toThrow(/must be of type "Object"/);

expect(() => {
extensions.validateExtension(extensions.EXPLORER_ENABLED, Oas.init({ 'x-readme': null }));
}).to.throw(/must be of type "Object"/);
}).toThrow(/must be of type "Object"/);
});

[
describe.each([
[
'CODE_SAMPLES',
[
Expand All @@ -174,55 +170,52 @@ describe('oas-extensions', function () {
['SAMPLES_LANGUAGES', ['swift'], {}, 'Array'],
['SEND_DEFAULTS', true, 'absolutely not', 'Boolean'],
['SIMPLE_MODE', true, 'absolutely not', 'Boolean'],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
].forEach(([extension, validValue, invalidValue, expectedType]: [string, any, any, string]) => {
describe(`${extension}`, function () {
describe('should allow valid extensions', function () {
it('should allow at the root level', function () {
expect(() => {
extensions.validateExtension(
extensions[extension],
Oas.init({ [`x-${extensions[extension]}`]: validValue })
);
}).not.to.throw();
});

it('should allow if nested in `x-readme`', function () {
expect(() => {
extensions.validateExtension(
extensions[extension],
Oas.init({
'x-readme': {
[extensions[extension]]: validValue,
},
})
);
}).not.to.throw();
});
])('%s', (extension, validValue, invalidValue, expectedType) => {
describe('should allow valid extensions', function () {
it('should allow at the root level', function () {
expect(() => {
extensions.validateExtension(
extensions[extension],
Oas.init({ [`x-${extensions[extension]}`]: validValue })
);
}).not.toThrow();
});

it('should allow if nested in `x-readme`', function () {
expect(() => {
extensions.validateExtension(
extensions[extension],
Oas.init({
'x-readme': {
[extensions[extension]]: validValue,
},
})
);
}).not.toThrow();
});
});

describe('should fail on invalid extension values', function () {
it('should error if at the root level', function () {
expect(() => {
extensions.validateExtension(
extensions[extension],
Oas.init({ [`x-${extensions[extension]}`]: invalidValue })
);
}).toThrow(new RegExp(`"x-${extensions[extension]}" must be of type "${expectedType}"`));
});

describe('should fail on invalid extension values', function () {
it('should error if at the root level', function () {
expect(() => {
extensions.validateExtension(
extensions[extension],
Oas.init({ [`x-${extensions[extension]}`]: invalidValue })
);
}).to.throw(new RegExp(`"x-${extensions[extension]}" must be of type "${expectedType}"`));
});

it('should error if nested in `x-readme`', function () {
expect(() => {
extensions.validateExtension(
extensions[extension],
Oas.init({
'x-readme': {
[extensions[extension]]: invalidValue,
},
})
);
}).to.throw(new RegExp(`"x-readme.${extensions[extension]}" must be of type "${expectedType}"`));
});
it('should error if nested in `x-readme`', function () {
expect(() => {
extensions.validateExtension(
extensions[extension],
Oas.init({
'x-readme': {
[extensions[extension]]: invalidValue,
},
})
);
}).toThrow(new RegExp(`"x-readme.${extensions[extension]}" must be of type "${expectedType}"`));
});
});
});
Expand Down
2 changes: 0 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
// "module": "es6",
// "moduleResolution": "node",
"noImplicitAny": false,
},
"include": ["../src/**/*", "*.ts", "**/*"],
Expand Down

0 comments on commit a0ea00f

Please sign in to comment.