Skip to content

Commit

Permalink
Convert from chai to jest (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jan 5, 2020
1 parent af3e629 commit dec4cdf
Show file tree
Hide file tree
Showing 45 changed files with 734 additions and 800 deletions.
425 changes: 196 additions & 229 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,36 @@
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"devDependencies": {
"@types/chai": "^4.2.7",
"@types/comment-json": "^1.1.1",
"@types/configstore": "^4.0.0",
"@types/fs-extra": "^8.0.1",
"@types/glob": "^7.1.1",
"@types/jest": "^24.0.24",
"@types/jest": "^24.0.25",
"@types/lorem-ipsum": "^1.0.2",
"@types/micromatch": "^3.1.1",
"@types/minimatch": "^3.0.3",
"@types/node": "^10.17.11",
"@types/xregexp": "^3.0.30",
"ajv-cli": "^3.0.0",
"chai": "^4.2.0",
"coveralls": "^3.0.9",
"cspell": "^4.0.41",
"cspell-dict-en_us": "^1.2.23",
"cspell-dict-es-es": "^1.0.17",
"cspell-dict-nl-nl": "^1.0.22",
"cspell-tools": "^4.1.9",
"cspell": "^4.0.44",
"cspell-dict-en_us": "^1.2.24",
"cspell-dict-es-es": "^1.0.18",
"cspell-dict-nl-nl": "^1.0.23",
"cspell-tools": "^4.1.12",
"fs-extra": "^8.1.0",
"globcat": "^1.2.0",
"jest": "^24.9.0",
"lerna": "^3.19.0",
"lerna": "^3.20.2",
"lorem-ipsum": "^1.0.6",
"rimraf": "^3.0.0",
"source-map-support": "^0.5.16",
"ts-jest": "^24.2.0",
"ts-json-schema-generator": "^0.55.0",
"ts-json-schema-generator": "^0.58.1",
"ts-loader": "^6.2.1",
"tslint": "^5.20.1",
"typescript": "^3.7.4",
"webpack": "^4.41.4",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
},
"nyc": {
Expand Down
7 changes: 3 additions & 4 deletions packages/cspell-glob/src/GlobMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { GlobMatcher, GlobMatch } from './GlobMatcher';
import { expect } from 'chai';

describe('Validate GlobMatcher', () => {
tests().forEach(([patterns, root, filename, expected, description], index) => {
test(
`test ${index} ${description}, pattern: [${patterns}] filename: "${filename}", root: "${root}"`,
() => {
const matcher = new GlobMatcher(patterns, root);
expect(matcher.match(filename)).to.be.eq(expected);
expect(matcher.match(filename)).toBe(expected);
}
);
});
Expand All @@ -32,7 +31,7 @@ describe('Tests .gitignore file contents', () => {
test(
`Test: "${comment}" File: "${filename}" (${expected ? 'Block' : 'Allow'}) `,
() => {
expect(matcher.match(filename)).to.be.eq(expected);
expect(matcher.match(filename)).toBe(expected);
}
);
}
Expand All @@ -41,7 +40,7 @@ describe('Tests .gitignore file contents', () => {
test(
`Test: "${comment}" File: "${filename}" (${expected ? 'Block' : 'Allow'}) `,
() => {
expect(matcher.matchEx(filename)).to.be.deep.eq(expected);
expect(matcher.matchEx(filename)).toEqual(expected);
}
);
}
Expand Down
22 changes: 11 additions & 11 deletions packages/cspell-io/src/file/fileReader.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { expect } from 'chai';
import * as fReader from './fileReader';
import * as fs from 'fs-extra';
import * as path from 'path';
Expand All @@ -17,64 +16,65 @@ describe('Validate the fileReader', () => {
test('tests reading a file', async () => {
const expected = await fs.readFile(__filename, 'utf8');
const result = await fReader.readFile(__filename, 'utf8');
expect(result).to.be.equal(expected);
expect(result).toBe(expected);
});

test('tests stringsToLines', async () => {
const strings = stringToStream('a1\n2\n3\n4', '5\n6');
const a = await asyncIterable.toArray(fReader.streamLineByLineAsync(strings));
expect(a).to.be.deep.equal(['a1', '2', '3', '45', '6']);
expect(a).toEqual(['a1', '2', '3', '45', '6']);
});

test('tests stringsToLines trailing new line', async () => {
const strings = stringToStream('a1\n2\n3\n4', '5\n6\n');
const a = await asyncIterable.toArray(fReader.streamLineByLineAsync(strings));
expect(a).to.be.deep.equal(['a1', '2', '3', '45', '6', '']);
expect(a).toEqual(['a1', '2', '3', '45', '6', '']);
});

test('test the file reader', async () => {
const lines = await asyncIterable.toArray(fReader.streamFileLineByLineAsync(__filename));
const actual = lines.join('\n');
const expected = fs.readFileSync(__filename, 'UTF-8');
expect(actual).to.equal(expected);
expect(actual).toBe(expected);
});

test('test the lineReaderAsync', async () => {
const lines = await asyncIterable.toArray(fReader.lineReaderAsync(__filename));
const expected = fs.readFileSync(__filename, 'UTF-8').split('\n');
expect(lines).to.deep.equal(expected);
expect(lines).toEqual(expected);
});

test('tests reading the cities sample', async () => {
const lines = await asyncIterable.toArray(fReader.lineReaderAsync(fileCities));
const file = await fs.readFile(fileCities, 'utf8');
expect(lines).to.be.deep.equal(file.split('\n'));
expect(lines).toEqual(file.split('\n'));
});

test('tests streamFileLineByLineAsync', async () => {
await Promise.all(sampleFiles
.map(async filename => {
const lines = await asyncIterable.toArray(fReader.streamFileLineByLineAsync(filename));
const file = await fs.readFile(filename, 'utf8');
expect(lines, `compare to file: ${filename}`).to.be.deep.equal(file.split(/\r?\n/));
// compare to file: ${filename}
expect(lines).toEqual(file.split(/\r?\n/));
}));
});

test('tests streamFileLineByLineAsync 2', async () => {
const lines = await asyncIterable.toArray(fReader.streamFileLineByLineAsync(__filename));
const file = await fs.readFile(__filename, 'utf8');
expect(lines).to.be.deep.equal(file.split('\n'));
expect(lines).toEqual(file.split('\n'));
});

test('test missing file', async () => {
const result = asyncIterable.toArray(fReader.lineReaderAsync(__filename + 'not.found'));
return result.then(
() => {
expect('not to be here').to.be.true;
expect('not to be here').toBe(true);
},
(e) => {
// expect(e).to.be.instanceof(Error); // Since jest currently mocks Error, this test fails.
expect(e.code).to.be.equal('ENOENT');
expect(e.code).toBe('ENOENT');
}
);
});
Expand Down
9 changes: 4 additions & 5 deletions packages/cspell-io/src/file/fileWriter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { expect } from 'chai';
import * as fileWriter from './fileWriter';
import * as loremIpsum from 'lorem-ipsum';
import * as path from 'path';
Expand All @@ -15,7 +14,7 @@ describe('Validate the writer', () => {
await mkdirp(path.dirname(filename));
await fileWriter.writeToFileIterableP(filename, data);
const result = await readFile(filename, 'utf8');
expect(result).to.equal(text);
expect(result).toBe(text);
});

test('tests writing data and reading it back. gz', async () => {
Expand All @@ -26,7 +25,7 @@ describe('Validate the writer', () => {
await mkdirp(path.dirname(filename));
await fileWriter.writeToFileIterableP(filename, data);
const result = await readFile(filename, 'utf8');
expect(result).to.equal(text);
expect(result).toBe(text);
});

test('tests writeToFile', async () => {
Expand All @@ -41,7 +40,7 @@ describe('Validate the writer', () => {
});

const result = await readFile(filename, 'utf8');
expect(result).to.equal(text);
expect(result).toBe(text);
});

test('tests writeToFile zip', async () => {
Expand All @@ -56,6 +55,6 @@ describe('Validate the writer', () => {
});

const result = await readFile(filename, 'utf8');
expect(result).to.equal(text);
expect(result).toBe(text);
});
});
2 changes: 1 addition & 1 deletion packages/cspell-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"build-schema": "ts-json-schema-generator -r --path src/Settings/CSpellSettingsDef.ts --type CSpellSettings > ../../cspell.schema.json",
"clean-build": "npm run clean && npm run build && npm run build-dictionaries",
"build-dictionaries": "npm run build_dictionaries-word-lists",
"build_dictionaries-word-lists": "node ../../node_modules/cspell-tools/dist/app.js compile \"./dictionaries/!(words)*.txt\" -o ./dist/dictionaries/",
"build_dictionaries-word-lists": "node ../../node_modules/cspell-tools/bin.js compile \"./dictionaries/!(words)*.txt\" -o ./dist/dictionaries/",
"compile": "tsc -p .",
"watch": "tsc --watch -p .",
"tsc": "tsc -p .",
Expand Down
11 changes: 5 additions & 6 deletions packages/cspell-lib/src/LanguageIds.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {expect} from 'chai';
import * as LangId from './LanguageIds';
import {genSequence} from 'gensequence';

describe('Validate LanguageIds', () => {
test('tests looking up a few extensions', () => {
expect(LangId.getLanguagesForExt('ts')).to.contain('typescript');
expect(LangId.getLanguagesForExt('.tex')).to.contain('latex');
expect(LangId.getLanguagesForExt('tex')).to.contain('latex');
expect(LangId.getLanguagesForExt('hs')).to.contain('haskell');
expect(LangId.getLanguagesForExt('ts')).toEqual(expect.arrayContaining(['typescript']));
expect(LangId.getLanguagesForExt('.tex')).toEqual(expect.arrayContaining(['latex']));
expect(LangId.getLanguagesForExt('tex')).toEqual(expect.arrayContaining(['latex']));
expect(LangId.getLanguagesForExt('hs')).toEqual(expect.arrayContaining(['haskell']));
});

test('test that all extensions start with a .', () => {
const ids = LangId.buildLanguageExtensionMap(LangId.languageExtensionDefinitions);
const badExtensions = genSequence(ids.keys())
.filter(ext => ext[0] !== '.')
.toArray();
expect(badExtensions, 'All extensions are expected to begin with a .').to.be.empty;
expect(Object.keys(badExtensions)).toHaveLength(0);
});

});
56 changes: 28 additions & 28 deletions packages/cspell-lib/src/Settings/CSpellSettingsServer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { expect } from 'chai';
import {
mergeSettings,
readSettings,
Expand All @@ -17,7 +16,7 @@ describe('Validate CSpellSettingsServer', () => {
test('tests mergeSettings', () => {
const left = { name: 'Left' };
const right = { name: 'Right' };
expect(mergeSettings(left, right)).to.be.deep.equal({
expect(mergeSettings(left, right)).toEqual({
words: [],
name: 'Left|Right',
id: '|',
Expand All @@ -37,7 +36,7 @@ describe('Validate CSpellSettingsServer', () => {
test('tests mergeSettings', () => {
const left = { id: 'left' };
const enabled = { id: 'enabledId', name: 'enabledName', enabled: true};
expect(mergeSettings(left, enabled)).to.be.deep.equal({
expect(mergeSettings(left, enabled)).toEqual({
enabled: true,
name: '|enabledName',
id: 'left|enabledId',
Expand All @@ -58,9 +57,9 @@ describe('Validate CSpellSettingsServer', () => {
test('tests mergeSettings', () => {
const right = { id: 'right', enabled: false };
const left = { id: 'left', enabled: true };
expect(mergeSettings({}, right)).to.be.deep.equal(right);
expect(mergeSettings(left, {})).to.be.deep.equal(left);
expect(mergeSettings(left, right)).to.be.deep.equal({
expect(mergeSettings({}, right)).toEqual(right);
expect(mergeSettings(left, {})).toEqual(left);
expect(mergeSettings(left, right)).toEqual({
enabled: right.enabled,
name: '|',
id: [left.id, right.id].join('|'),
Expand All @@ -79,7 +78,7 @@ describe('Validate CSpellSettingsServer', () => {
});

test('tests mergeSettings', () => {
expect(mergeSettings({enabled: true}, {enabled: false})).to.be.deep.equal({
expect(mergeSettings({enabled: true}, {enabled: false})).toEqual({
enabled: false,
name: '|',
id: '|',
Expand All @@ -98,76 +97,76 @@ describe('Validate CSpellSettingsServer', () => {
});

test('tests mergeSettings when left/right are the same', () => {
expect(mergeSettings(_defaultSettings, _defaultSettings)).to.be.equal(_defaultSettings);
expect(mergeSettings(_defaultSettings, _defaultSettings)).toBe(_defaultSettings);
});

test('tests mergeSettings when lefts are the same', () => {
const base = mergeSettings(_defaultSettings, {});
const setting1 = mergeSettings(base, {});
const setting2 = mergeSettings(base, setting1);
expect(setting2).to.be.equal(setting1);
expect(setting2).toBe(setting1);

const setting3 = mergeSettings(_defaultSettings, setting1);
expect(setting3).to.be.equal(setting1);
expect(setting3).toBe(setting1);
});

test('tests mergeSettings when rights are the same', () => {
const base = mergeSettings(_defaultSettings, { id: 'right' });
const setting1 = mergeSettings({ id: 'setting1' }, base);
const setting2 = mergeSettings(setting1, base);
expect(setting2).to.be.equal(setting1);
expect(setting2).toBe(setting1);

const setting3 = mergeSettings(_defaultSettings, setting1);
expect(setting3).to.be.not.equal(setting1);
expect(setting3).not.toBe(setting1);

const setting4 = mergeSettings(setting3, base);
expect(setting4).to.be.equal(setting3);
expect(setting4).toBe(setting3);
});

test('tests loading a missing cSpell.json file', () => {
const filename = path.join(__dirname, '..', '..', 'samples', 'linked', 'cspell-missing.json');
const settings = readSettings(filename);
expect(settings).to.not.be.empty;
expect(settings.words).to.be.undefined;
expect(Object.keys(settings)).not.toHaveLength(0);
expect(settings.words).toBeUndefined();
});

test('tests loading a cSpell.json file', () => {
const filename = path.join(__dirname, '..', '..', 'samples', 'linked', 'cspell-import.json');
const settings = readSettings(filename);
expect(settings).to.not.be.empty;
expect(settings.words).to.include('import');
expect(Object.keys(settings)).not.toHaveLength(0);
expect(settings.words).toEqual(expect.arrayContaining(['import']));
});

test('tests loading a cSpell.json with multiple imports file', () => {
const filename = path.join(__dirname, '..', '..', 'samples', 'linked', 'cspell-imports.json');
const settings = readSettings(filename);
expect(settings).to.not.be.empty;
expect(settings.words).to.include('import');
expect(settings.words).to.include('imports');
expect(Object.keys(settings)).not.toHaveLength(0);
expect(settings.words).toEqual(expect.arrayContaining(['import']));
expect(settings.words).toEqual(expect.arrayContaining(['imports']));
// cspell:word leuk
expect(settings.words).to.include('leuk');
expect(settings.words).toEqual(expect.arrayContaining(['leuk']));
});

test('makes sure global settings is an object', () => {
const settings = getGlobalSettings();
expect(settings).to.not.be.empty;
expect(Object.keys(settings)).not.toHaveLength(0);
const merged = mergeSettings(getDefaultSettings(), getGlobalSettings());
expect(merged).to.not.be.empty;
expect(Object.keys(merged)).not.toHaveLength(0);
});

test('verify clearing the file cache works', () => {
mergeSettings(getDefaultSettings(), getGlobalSettings());
expect(getCachedFileSize()).to.be.gt(0);
expect(getCachedFileSize()).toBeGreaterThan(0);
clearCachedFiles();
expect(getCachedFileSize()).to.be.eq(0);
expect(getCachedFileSize()).toBe(0);
});

test('test the loaded defaults contain expected settings', () => {
const settings = getDefaultSettings();
const sources = getSources(settings);
const sourceNames = sources
.map(s => s.name || '?');
expect(sourceNames).to.contain(_defaultSettings.name!);
expect(sourceNames).toEqual(expect.arrayContaining([_defaultSettings.name!]));
});
});

Expand All @@ -185,7 +184,8 @@ describe('Validate Overrides', () => {
{ f: '/cspell-dicts/nl_NL/Dutch.txt', g: '**/nl_NL/**', e: true },
];

tests.forEach(({f, g, e}) => expect(checkFilenameMatchesGlob(f, g), `f: ${f}, g: ${g}, e: ${e}`).to.be.equal(e));
tests.forEach(({f, g, e}) => // f: ${f}, g: ${g}, e: ${e}
expect(checkFilenameMatchesGlob(f, g)).toBe(e));
});

test('test calcOverrideSettings', () => {
Expand All @@ -196,7 +196,7 @@ describe('Validate Overrides', () => {

tests.forEach(({f, e}) => {
const r = calcOverrideSettings(sampleSettings, f);
e.forEach(([k, v]) => expect(r[k]).to.be.equal(v));
e.forEach(([k, v]) => expect(r[k]).toBe(v));
});
});
});
Expand Down
Loading

0 comments on commit dec4cdf

Please sign in to comment.