Skip to content

Commit

Permalink
migrate to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Sep 22, 2024
1 parent 84871ba commit 4baec6d
Show file tree
Hide file tree
Showing 7 changed files with 2,104 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: yarn

- name: Log Versions
run: yarn tsc --version && yarn mocha --version
run: yarn tsc --version && yarn jest --version

- name: Prettier
run: yarn format:check
Expand Down
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: "node",
transform: {
"^.+.tsx?$": ["ts-jest",{}],
},
};
1 change: 0 additions & 1 deletion knip.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"ignore": [],
"ignoreBinaries": [],
"ignoreDependencies": [
"@types/mocha"
],
"ignoreExportsUsedInFile": true,
"project": [
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@
"temp": "^0.8.3"
},
"devDependencies": {
"@types/chai": "^4.3.19",
"@types/errorhandler": "^0.0.32",
"@types/escape-html": "^1.0.4",
"@types/express": "^4.0.39",
"@types/fs-extra": "^4.0.5",
"@types/jest": "^29.5.13",
"@types/lodash": "^4.14.87",
"@types/mocha": "^2.2.44",
"@types/node": "18",
"@types/reservoir": "^0.1.6",
"@types/temp": "^0.8.31",
"chai": "^5.1.1",
"jest": "^29.7.0",
"knip": "^5.30.4",
"mocha": "^4.0.1",
"prettier": "^3.3.3",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.6.2"
},
"scripts": {
"demo": "ts-node localturk.ts --random-order sample/transcribe.html sample/tasks.csv sample/outputs.csv",
"format": "prettier --write '*.ts' 'test/*.ts'",
"format:check": "prettier --check '*.ts' 'test/*.ts'",
"tsc": "tsc",
"test": "mocha --require ts-node/register test/**/*.ts"
"test": "jest"
},
"prettier": {
"arrowParens": "avoid",
Expand Down
32 changes: 15 additions & 17 deletions test/csv_test.ts → test/csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@ import * as fs from 'fs-extra';

import * as csv from '../csv';

import {expect} from 'chai';

describe('csv', () => {
it('should read CSV headers', async () => {
const headers = await csv.readHeaders('./test/test.csv');
expect(headers).to.deep.equal(['id', 'First', 'Last']);
expect(headers).toEqual(['id', 'First', 'Last']);
});

it('should read quoted CSV headers', async () => {
const headers = await csv.readHeaders('./test/quoted.csv');
expect(headers).to.deep.equal(['id', 'First Name', 'Last,Name']);
expect(headers).toEqual(['id', 'First Name', 'Last,Name']);
});

it('should read lines in a simple CSV file', async () => {
const rows = [];
for await (const row of csv.readRows('./test/test.csv')) {
rows.push(row);
}
expect(rows).to.deep.equal([
expect(rows).toEqual([
['id', 'First', 'Last'],
['1', 'Jane', 'Doe'],
['2', 'John', 'Doer'],
Expand All @@ -32,7 +30,7 @@ describe('csv', () => {
for await (const row of csv.readRows('./test/quoted.csv')) {
rows.push(row);
}
expect(rows).to.deep.equal([
expect(rows).toEqual([
['id', 'First Name', 'Last,Name'],
['1', 'Jane\nDoe', 'Doe'],
['2', 'John', 'Doer\nQuoter'],
Expand All @@ -44,7 +42,7 @@ describe('csv', () => {
for await (const row of csv.readRows('./sample/outputs.csv')) {
rows.push(row);
}
expect(rows).to.deep.equal([
expect(rows).toEqual([
['image1', 'image2', 'line1', 'line2'],
[
'images/0.png',
Expand All @@ -70,7 +68,7 @@ describe('csv', () => {
];
await csv.writeCsv('/tmp/test.csv', rows);
const data = await read('/tmp/test.csv');
expect(data).to.equal('id,First,Last\n' + '1,Jane,Doe\n' + '2,Dan,VK\n');
expect(data).toEqual('id,First,Last\n' + '1,Jane,Doe\n' + '2,Dan,VK\n');
});

it('should write a complex CSV file', async () => {
Expand All @@ -81,32 +79,32 @@ describe('csv', () => {
];
await csv.writeCsv('/tmp/test.csv', rows);
const data = await read('/tmp/test.csv');
expect(data).to.equal(
expect(data).toEqual(
'id,"First,Last","Last,First"\n' + '1,"Jane,Doe","Doe,Jane"\n' + '2,"Dan,\nVK","VK,Dan"\n',
);
});

it('should write a fresh CSV file', async () => {
await ensureDeleted('/tmp/test.csv');
await csv.appendRow('/tmp/test.csv', {a: '1', b: '2'});
expect(await read('/tmp/test.csv')).to.equal('a,b\n1,2\n');
expect(await read('/tmp/test.csv')).toEqual('a,b\n1,2\n');
});

it('should append to a CSV file', async () => {
await ensureDeleted('/tmp/test.csv');
await csv.appendRow('/tmp/test.csv', {a: '1', b: '2'});
await csv.appendRow('/tmp/test.csv', {b: '1', a: '2'});
expect(await read('/tmp/test.csv')).to.equal('a,b\n1,2\n2,1\n');
expect(await read('/tmp/test.csv')).toEqual('a,b\n1,2\n2,1\n');
});

it('should append a column to a CSV file', async () => {
await ensureDeleted('/tmp/test.csv');
await csv.appendRow('/tmp/test.csv', {a: '1', b: '2'});
expect(await read('/tmp/test.csv')).to.equal('a,b\n1,2\n');
expect(await read('/tmp/test.csv')).toEqual('a,b\n1,2\n');
await csv.appendRow('/tmp/test.csv', {b: '1', a: '2'});
expect(await read('/tmp/test.csv')).to.equal('a,b\n1,2\n2,1\n');
expect(await read('/tmp/test.csv')).toEqual('a,b\n1,2\n2,1\n');
await csv.appendRow('/tmp/test.csv', {b: '3', c: '4'});
expect(await read('/tmp/test.csv')).to.equal('a,b,c\n1,2,\n2,1,\n,3,4\n');
expect(await read('/tmp/test.csv')).toEqual('a,b,c\n1,2,\n2,1,\n,3,4\n');
});

it('should append to a CSV file without trailing newline', async () => {
Expand All @@ -121,7 +119,7 @@ describe('csv', () => {
for await (const row of csv.readRows('/tmp/test.csv')) {
rows.push(row);
}
expect(rows).to.deep.equal([
expect(rows).toEqual([
['image1', 'image2', 'line1', 'line2'],
[
'images/0.png',
Expand All @@ -141,8 +139,8 @@ describe('csv', () => {
];
await csv.writeCsv('/tmp/test.csv', rows);
const deleted = await csv.deleteLastRow('/tmp/test.csv');
expect(deleted).to.deep.equal(['2', 'Dan,\nVK', 'VK,Dan']);
expect(deleted).toEqual(['2', 'Dan,\nVK', 'VK,Dan']);
const data = await read('/tmp/test.csv');
expect(data).to.equal('id,"First,Last","Last,First"\n' + '1,"Jane,Doe","Doe,Jane"\n');
expect(data).toEqual('id,"First,Last","Last,First"\n' + '1,"Jane,Doe","Doe,Jane"\n');
});
});
20 changes: 9 additions & 11 deletions test/utils_test.ts → test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import * as utils from '../utils';

import {expect} from 'chai';

describe('utils', () => {
it('should render a template', () => {
expect(utils.renderTemplate('blah ${x} blah', {x: 'blah'})).to.equal('blah blah blah');
expect(utils.renderTemplate('blah ${x} blah', {x: 'blah'})).toEqual('blah blah blah');
});

it('should render the same value repeatedly', () => {
expect(utils.renderTemplate('${x} ${x} ${x}', {x: 'blah'})).to.equal('blah blah blah');
expect(utils.renderTemplate('${x} ${x} ${x}', {x: 'blah'})).toEqual('blah blah blah');
});

it('should escape html entities', () => {
expect(utils.htmlEntities('&<>')).to.equal('&amp;&lt;&gt;');
expect(utils.htmlEntities('&<>')).toEqual('&amp;&lt;&gt;');
});

it('should normalize newlines', () => {
Expand All @@ -22,18 +20,18 @@ describe('utils', () => {
b: 'foo\r\nbar',
c: 'foo\rbar',
}),
).to.deep.equal({
).toEqual({
a: 'foo\nbar',
b: 'foo\nbar',
c: 'foo\nbar',
});
});

it('should determine supersets', () => {
expect(utils.isSupersetOf({}, {})).to.be.true;
expect(utils.isSupersetOf({a: 1}, {a: 1})).to.be.true;
expect(utils.isSupersetOf({a: 1, b: 2}, {a: 1})).to.be.true;
expect(utils.isSupersetOf({a: 1}, {a: 1, b: 2})).to.be.false;
expect(utils.isSupersetOf({a: 1}, {a: 2})).to.be.false;
expect(utils.isSupersetOf({}, {})).toBe(true);
expect(utils.isSupersetOf({a: 1}, {a: 1})).toBe(true);
expect(utils.isSupersetOf({a: 1, b: 2}, {a: 1})).toBe(true);
expect(utils.isSupersetOf({a: 1}, {a: 1, b: 2})).toBe(false);
expect(utils.isSupersetOf({a: 1}, {a: 2})).toBe(false);
});
});
Loading

0 comments on commit 4baec6d

Please sign in to comment.