Skip to content

Commit

Permalink
Switch from mocha/chai to jest for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Mar 7, 2023
1 parent f26d6a7 commit 69e3b0f
Show file tree
Hide file tree
Showing 9 changed files with 6,694 additions and 1,869 deletions.
16 changes: 16 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
// SPDX-FileCopyrightText: Ansible Project
// SPDX-License-Identifier: BSD-2-Clause

export default {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts': [
'ts-jest',
{
isolatedModules: true,
},
],
},
};
8,403 changes: 6,608 additions & 1,795 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 @@ -8,7 +8,6 @@
"files": [
"dist",
"src",
"tests",
"tsconfig.json",
"CHANGELOG.rst",
"LICENSE",
Expand All @@ -19,8 +18,8 @@
"build": "npx tsc",
"format:check": "npx prettier --check .",
"format:write": "npx prettier --write .",
"lint": "npx eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
"test": "npx ts-mocha tests/**/*.test.ts",
"lint": "npx eslint \"src/**/*.ts\"",
"test": "npx jest",
"watch": "npx tsc -w"
},
"keywords": [
Expand All @@ -31,15 +30,13 @@
"license": "BSD-2-Clause",
"repository": "https://github.com/ansible-community/antsibull-docs-ts",
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/mocha": "^10.0.1",
"@types/jest": "^29.4.0",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"chai": "^4.3.7",
"eslint": "^8.35.0",
"mocha": "^10.2.0",
"jest": "^29.5.0",
"prettier": "^2.8.4",
"ts-mocha": "^10.0.0",
"ts-jest": "^29.0.5",
"typescript": "^4.9.5",
"yaml": "^2.2.1"
}
Expand Down
14 changes: 6 additions & 8 deletions tests/html.test.ts → src/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@
SPDX-License-Identifier: BSD-2-Clause
*/

import { expect } from 'chai';

import * as parser from '../src/parser';
import { quoteHTML, toHTML } from '../src/html';
import * as parser from './parser';
import { quoteHTML, toHTML } from './html';

describe('quoteHTML tests', (): void => {
it('empty string', (): void => {
expect(quoteHTML('')).is.equal('');
expect(quoteHTML('')).toBe('');
});
it('simple string', (): void => {
expect(quoteHTML('foo')).is.equal('foo');
expect(quoteHTML('foo')).toBe('foo');
});
it('more complex', (): void => {
expect(quoteHTML('<a href="a&b">&lt;&amp;&gt;</a>')).is.equal(
expect(quoteHTML('<a href="a&b">&lt;&amp;&gt;</a>')).toBe(
'&lt;a href="a&amp;b"&gt;&amp;lt;&amp;amp;&amp;gt;&lt;/a&gt;',
);
});
});

describe('toHTML tests', (): void => {
it('no paragraphs', (): void => {
expect(toHTML([[{ type: parser.PartType.TEXT, text: 'test' }]])).is.equal('<p>test</p>');
expect(toHTML([[{ type: parser.PartType.TEXT, text: 'test' }]])).toBe('<p>test</p>');
});
});
74 changes: 40 additions & 34 deletions tests/parser.test.ts → src/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@
SPDX-License-Identifier: BSD-2-Clause
*/

import { expect } from 'chai';

import { parse, PartType } from '../src/parser';
import { parse, PartType } from './parser';

describe('parser tests', (): void => {
it('empty string', (): void => {
expect(parse('')).to.deep.equal([]);
expect(parse('')).toEqual([]);
});
it('array with empty string', (): void => {
expect(parse([''])).to.deep.equal([[]]);
expect(parse([''])).toEqual([[]]);
});
it('simple string', (): void => {
expect(parse('test')).to.deep.equal([[{ type: PartType.TEXT, text: 'test' }]]);
expect(parse('test')).toEqual([[{ type: PartType.TEXT, text: 'test' }]]);
});
it('basic markup test', (): void => {
expect(
parse(
'foo I(bar) baz C( bam ) B( ( boo ) ) U(https://example.com/?foo=bar)L(foo , https://bar.com) R( a , b )M(foo.bar.baz)x M(foo.bar.baz.bam)',
),
).to.deep.equal([
).toEqual([
[
{ type: PartType.TEXT, text: 'foo ' },
{ type: PartType.ITALIC, text: 'bar' },
Expand All @@ -43,61 +41,69 @@ describe('parser tests', (): void => {
]);
});
it('bad parameter parsing (no escaping, throw error)', (): void => {
expect(() => parse('M(')).to.throw('While parsing M() at index 1: Cannot find closing ")" after last parameter');
expect(() => parse('M(foo')).to.throw('While parsing M() at index 1: Cannot find closing ")" after last parameter');
expect(() => parse('L(foo)')).to.throw(
expect(async () => parse('M(')).rejects.toThrow(
'While parsing M() at index 1: Cannot find closing ")" after last parameter',
);
expect(async () => parse('M(foo')).rejects.toThrow(
'While parsing M() at index 1: Cannot find closing ")" after last parameter',
);
expect(async () => parse('L(foo)')).rejects.toThrow(
'While parsing L() at index 1: Cannot find comma separating parameter 1 from the next one',
);
expect(() => parse('L(foo,bar')).to.throw(
expect(async () => parse('L(foo,bar')).rejects.toThrow(
'While parsing L() at index 1: Cannot find closing ")" after last parameter',
);
expect(() => parse('L(foo), bar')).to.throw(
expect(async () => parse('L(foo), bar')).rejects.toThrow(
'While parsing L() at index 1: Cannot find closing ")" after last parameter',
);
});
it('bad module ref (throw error)', (): void => {
expect(() => parse('M(foo)')).to.throw('While parsing M() at index 1: Error: Module name "foo" is not a FQCN');
expect(() => parse(' M(foo.bar)')).to.throw(
expect(async () => parse('M(foo)')).rejects.toThrow(
'While parsing M() at index 1: Error: Module name "foo" is not a FQCN',
);
expect(async () => parse(' M(foo.bar)')).rejects.toThrow(
'While parsing M() at index 2: Error: Module name "foo.bar" is not a FQCN',
);
expect(() => parse(' M(foo. bar.baz)')).to.throw(
expect(async () => parse(' M(foo. bar.baz)')).rejects.toThrow(
'While parsing M() at index 3: Error: Module name "foo. bar.baz" is not a FQCN',
);
expect(() => parse(' M(foo)')).to.throw('While parsing M() at index 4: Error: Module name "foo" is not a FQCN');
expect(async () => parse(' M(foo)')).rejects.toThrow(
'While parsing M() at index 4: Error: Module name "foo" is not a FQCN',
);
});
it('bad parameter parsing (no escaping, error message)', (): void => {
expect(parse('M(', { errors: 'message' })).to.deep.equal([
expect(parse('M(', { errors: 'message' })).toEqual([
[{ type: PartType.ERROR, message: 'While parsing M() at index 1: Cannot find closing ")" after last parameter' }],
]);
expect(parse('M(foo', { errors: 'message' })).to.deep.equal([
expect(parse('M(foo', { errors: 'message' })).toEqual([
[{ type: PartType.ERROR, message: 'While parsing M() at index 1: Cannot find closing ")" after last parameter' }],
]);
expect(parse('L(foo)', { errors: 'message' })).to.deep.equal([
expect(parse('L(foo)', { errors: 'message' })).toEqual([
[
{
type: PartType.ERROR,
message: 'While parsing L() at index 1: Cannot find comma separating parameter 1 from the next one',
},
],
]);
expect(parse('L(foo,bar', { errors: 'message' })).to.deep.equal([
expect(parse('L(foo,bar', { errors: 'message' })).toEqual([
[{ type: PartType.ERROR, message: 'While parsing L() at index 1: Cannot find closing ")" after last parameter' }],
]);
expect(parse('L(foo), bar', { errors: 'message' })).to.deep.equal([
expect(parse('L(foo), bar', { errors: 'message' })).toEqual([
[{ type: PartType.ERROR, message: 'While parsing L() at index 1: Cannot find closing ")" after last parameter' }],
]);
});
it('bad module ref (error message)', (): void => {
expect(parse('M(foo)', { errors: 'message' })).to.deep.equal([
expect(parse('M(foo)', { errors: 'message' })).toEqual([
[{ type: PartType.ERROR, message: 'While parsing M() at index 1: Error: Module name "foo" is not a FQCN' }],
]);
expect(parse(' M(foo.bar)', { errors: 'message' })).to.deep.equal([
expect(parse(' M(foo.bar)', { errors: 'message' })).toEqual([
[
{ type: PartType.TEXT, text: ' ' },
{ type: PartType.ERROR, message: 'While parsing M() at index 2: Error: Module name "foo.bar" is not a FQCN' },
],
]);
expect(parse(' M(foo. bar.baz)', { errors: 'message' })).to.deep.equal([
expect(parse(' M(foo. bar.baz)', { errors: 'message' })).toEqual([
[
{ type: PartType.TEXT, text: ' ' },
{
Expand All @@ -106,7 +112,7 @@ describe('parser tests', (): void => {
},
],
]);
expect(parse(' M(foo) baz', { errors: 'message' })).to.deep.equal([
expect(parse(' M(foo) baz', { errors: 'message' })).toEqual([
[
{ type: PartType.TEXT, text: ' ' },
{ type: PartType.ERROR, message: 'While parsing M() at index 4: Error: Module name "foo" is not a FQCN' },
Expand All @@ -115,17 +121,17 @@ describe('parser tests', (): void => {
]);
});
it('bad parameter parsing (no escaping, ignore error)', (): void => {
expect(parse('M(', { errors: 'ignore' })).to.deep.equal([[]]);
expect(parse('M(foo', { errors: 'ignore' })).to.deep.equal([[]]);
expect(parse('L(foo)', { errors: 'ignore' })).to.deep.equal([[]]);
expect(parse('L(foo,bar', { errors: 'ignore' })).to.deep.equal([[]]);
expect(parse('L(foo), bar', { errors: 'ignore' })).to.deep.equal([[]]);
expect(parse('M(', { errors: 'ignore' })).toEqual([[]]);
expect(parse('M(foo', { errors: 'ignore' })).toEqual([[]]);
expect(parse('L(foo)', { errors: 'ignore' })).toEqual([[]]);
expect(parse('L(foo,bar', { errors: 'ignore' })).toEqual([[]]);
expect(parse('L(foo), bar', { errors: 'ignore' })).toEqual([[]]);
});
it('bad module ref (ignore error)', (): void => {
expect(parse('M(foo)', { errors: 'ignore' })).to.deep.equal([[]]);
expect(parse(' M(foo.bar)', { errors: 'ignore' })).to.deep.equal([[{ type: PartType.TEXT, text: ' ' }]]);
expect(parse(' M(foo. bar.baz)', { errors: 'ignore' })).to.deep.equal([[{ type: PartType.TEXT, text: ' ' }]]);
expect(parse(' M(foo) baz', { errors: 'ignore' })).to.deep.equal([
expect(parse('M(foo)', { errors: 'ignore' })).toEqual([[]]);
expect(parse(' M(foo.bar)', { errors: 'ignore' })).toEqual([[{ type: PartType.TEXT, text: ' ' }]]);
expect(parse(' M(foo. bar.baz)', { errors: 'ignore' })).toEqual([[{ type: PartType.TEXT, text: ' ' }]]);
expect(parse(' M(foo) baz', { errors: 'ignore' })).toEqual([
[
{ type: PartType.TEXT, text: ' ' },
{ type: PartType.TEXT, text: ' baz' },
Expand Down
22 changes: 10 additions & 12 deletions tests/rst.test.ts → src/rst.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,35 @@
SPDX-License-Identifier: BSD-2-Clause
*/

import { expect } from 'chai';

import * as parser from '../src/parser';
import { quoteRST, toRST } from '../src/rst';
import * as parser from './parser';
import { quoteRST, toRST } from './rst';

describe('quoteRST tests', (): void => {
it('empty string', (): void => {
expect(quoteRST('')).is.equal('');
expect(quoteRST('')).toBe('');
});
it('simple string', (): void => {
expect(quoteRST(' foo ')).is.equal(' foo ');
expect(quoteRST(' foo ')).toBe(' foo ');
});
it('simple string, escape leading spacing', (): void => {
expect(quoteRST(' foo ', true)).is.equal('\\ foo ');
expect(quoteRST(' foo ', true)).toBe('\\ foo ');
});
it('simple string, escape ending spacing', (): void => {
expect(quoteRST(' foo ', false, true)).is.equal(' foo \\ ');
expect(quoteRST(' foo ', false, true)).toBe(' foo \\ ');
});
it('simple string, escape spacing', (): void => {
expect(quoteRST(' foo ', true, true)).is.equal('\\ foo \\ ');
expect(quoteRST(' foo ', true, true)).toBe('\\ foo \\ ');
});
it('more complex', (): void => {
expect(quoteRST('\\<_>`*<_>*`\\')).is.equal('\\\\\\<\\_\\>\\`\\*\\<\\_\\>\\*\\`\\\\');
expect(quoteRST('\\<_>`*<_>*`\\')).toBe('\\\\\\<\\_\\>\\`\\*\\<\\_\\>\\*\\`\\\\');
});
});

describe('toRST tests', (): void => {
it('no paragraphs', (): void => {
expect(toRST([])).is.equal('');
expect(toRST([])).toBe('');
});
it('single paragraph with simple text', (): void => {
expect(toRST([[{ type: parser.PartType.TEXT, text: 'test' }]])).is.equal('test');
expect(toRST([[{ type: parser.PartType.TEXT, text: 'test' }]])).toBe('test');
});
});
18 changes: 7 additions & 11 deletions tests/vectors.test.ts → src/vectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
SPDX-License-Identifier: BSD-2-Clause
*/

import { expect } from 'chai';

import { readFileSync } from 'fs';

import { parse as parse_yaml } from 'yaml';

import { parse } from '../src/parser';
import { toHTML } from '../src/html';
import { toRST } from '../src/rst';
import { parse } from './parser';
import { toHTML } from './html';
import { toRST } from './rst';

describe('vectors', (): void => {
const data = readFileSync('tests/vectors.yaml', 'utf8');
const vectors: Any = parse_yaml(data).test_vectors;
const data = readFileSync('test-vectors.yaml', 'utf8');
const vectors = parse_yaml(data).test_vectors;
for (const test_name of Object.keys(vectors)) {
const test_data = vectors[test_name];
if (test_data.html_opts) {
Expand All @@ -26,14 +24,12 @@ describe('vectors', (): void => {
}
if (test_data.source !== undefined && test_data.html !== undefined) {
it(`${test_name} (Ansible doc => HTML)`, (): void => {
expect(toHTML(parse(test_data.source, test_data.parse_opts), test_data.html_opts)).to.deep.equal(
test_data.html,
);
expect(toHTML(parse(test_data.source, test_data.parse_opts), test_data.html_opts)).toEqual(test_data.html);
});
}
if (test_data.source !== undefined && test_data.rst !== undefined) {
it(`${test_name} (Ansible doc => RST)`, (): void => {
expect(toRST(parse(test_data.source, test_data.parse_opts), test_data.rst_opts)).to.deep.equal(test_data.rst);
expect(toRST(parse(test_data.source, test_data.parse_opts), test_data.rst_opts)).toEqual(test_data.rst);
});
}
}
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
/* Completeness */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": ["tests", "dist", "node_modules"]
"include": ["src/**/*"],
"exclude": ["src/**/*.spec.ts"]
}

0 comments on commit 69e3b0f

Please sign in to comment.