Skip to content

Commit 3c64380

Browse files
authored
Merge pull request microsoft#752 from Microsoft/pgonzal/ncl-jest
[node-core-library] Convert tests to use Jest
2 parents 90989c3 + 67b5a25 commit 3c64380

File tree

11 files changed

+196
-182
lines changed

11 files changed

+196
-182
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "",
5+
"packageName": "@microsoft/node-core-library",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/node-core-library",
10+
"email": "[email protected]"
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"isEnabled": true
3+
}

libraries/node-core-library/package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
"z-schema": "~3.18.3"
2121
},
2222
"devDependencies": {
23-
"@types/chai": "3.4.34",
24-
"@types/mocha": "2.2.38",
25-
"chai": "~3.5.0",
23+
"@types/jest": "21.1.10",
2624
"gulp": "~3.9.1",
27-
"mocha": "~3.4.2",
2825
"@microsoft/node-library-build": "4.3.41"
2926
}
3027
}

libraries/node-core-library/src/test/JsonSchema.test.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
/// <reference types='mocha' />
5-
6-
import { assert } from 'chai';
74
import * as path from 'path';
85
import { JsonFile } from '../JsonFile';
96
import { JsonSchema, IJsonSchemaErrorInfo } from '../JsonSchema';
@@ -18,14 +15,22 @@ describe('JsonSchema', () => {
1815

1916
const schema: JsonSchema = JsonSchema.fromFile(schemaPath);
2017

21-
it('loadAndValidate successfully validates a JSON file', (done: MochaDone) => {
18+
it('loadAndValidate successfully validates a JSON file', () => {
2219
const jsonPath: string = path.resolve(path.join(__dirname, './test-data/test.json'));
2320
const jsonObject: Object = JsonFile.loadAndValidate(jsonPath, schema);
24-
assert.isObject(jsonObject);
25-
done();
21+
expect(jsonObject).toMatchObject(
22+
{
23+
'exampleString': 'This is a string',
24+
'exampleArray': [
25+
'apple',
26+
'banana',
27+
'coconut'
28+
]
29+
}
30+
);
2631
});
2732

28-
it('validateObjectWithCallback successfully reports a compound validation error', (done: MochaDone) => {
33+
it('validateObjectWithCallback successfully reports a compound validation error', () => {
2934
const jsonPath2: string = path.resolve(path.join(__dirname, './test-data/test2.json'));
3035
const jsonObject2: Object = JsonFile.load(jsonPath2);
3136

@@ -41,12 +46,9 @@ Error: #/exampleOneOf (Description for type2)
4146

4247
schema.validateObjectWithCallback(jsonObject2, (errorInfo: IJsonSchemaErrorInfo) => {
4348
++errorCount;
44-
console.log(errorInfo.details);
45-
assert.equal(normalize(errorInfo.details), normalize(expectedError),
46-
'Error #' + errorCount.toString());
49+
expect(normalize(errorInfo.details)).toEqual(normalize(expectedError));
4750
});
4851

49-
assert.equal(errorCount, 1);
50-
done();
52+
expect(errorCount).toEqual(1);
5153
});
5254
});

libraries/node-core-library/src/test/LockFile.test.ts

+60-58
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
/// <reference types='mocha' />
5-
6-
import { assert } from 'chai';
74
import * as path from 'path';
85
import { LockFile, getProcessStartTime, getProcessStartTimeFromProcStat } from '../LockFile';
96
import { FileSystem } from '../FileSystem';
@@ -21,33 +18,34 @@ describe('LockFile', () => {
2118

2219
describe('getLockFilePath', () => {
2320
it('only acceps alphabetical characters for resource name', () => {
24-
assert.doesNotThrow(() => {
21+
expect(() => {
2522
LockFile.getLockFilePath(process.cwd(), 'foo123');
26-
});
27-
assert.doesNotThrow(() => {
23+
}).not.toThrow();
24+
expect(() => {
2825
LockFile.getLockFilePath(process.cwd(), 'bar.123');
29-
});
30-
assert.doesNotThrow(() => {
26+
}).not.toThrow();
27+
expect(() => {
3128
LockFile.getLockFilePath(process.cwd(), 'foo.bar');
32-
});
33-
assert.doesNotThrow(() => {
29+
}).not.toThrow();
30+
expect(() => {
3431
LockFile.getLockFilePath(process.cwd(), 'lock-file.123');
35-
});
36-
assert.throws(() => {
32+
}).not.toThrow();
33+
34+
expect(() => {
3735
LockFile.getLockFilePath(process.cwd(), '.foo123');
38-
});
39-
assert.throws(() => {
36+
}).toThrow();
37+
expect(() => {
4038
LockFile.getLockFilePath(process.cwd(), 'foo123.');
41-
});
42-
assert.throws(() => {
39+
}).toThrow();
40+
expect(() => {
4341
LockFile.getLockFilePath(process.cwd(), '-foo123');
44-
});
45-
assert.throws(() => {
42+
}).toThrow();
43+
expect(() => {
4644
LockFile.getLockFilePath(process.cwd(), 'foo123-');
47-
});
48-
assert.throws(() => {
45+
}).toThrow();
46+
expect(() => {
4947
LockFile.getLockFilePath(process.cwd(), '');
50-
});
48+
}).toThrow();
5149
});
5250
});
5351

@@ -63,50 +61,52 @@ describe('LockFile', () => {
6361
it('returns undefined if too few values are contained in /proc/[pid]/stat (1)', () => {
6462
const stat: string = createStatOutput('(bash)', 1);
6563
const ret: string|undefined = getProcessStartTimeFromProcStat(stat);
66-
assert.strictEqual(ret, undefined);
64+
expect(ret).toBeUndefined();
6765
});
6866
it('returns undefined if too few values are contained in /proc/[pid]/stat (2)', () => {
6967
const stat: string = createStatOutput('(bash)', 0);
7068
const ret: string|undefined = getProcessStartTimeFromProcStat(stat);
71-
assert.strictEqual(ret, undefined);
69+
expect(ret).toBeUndefined();
7270
});
7371
it('returns the correct start time if the second value in /proc/[pid]/stat contains spaces', () => {
7472
let stat: string = createStatOutput('(bash 2)', 18);
7573
const value22: string = '12345';
7674
stat += ` ${value22}`;
7775
const ret: string|undefined = getProcessStartTimeFromProcStat(stat);
78-
assert.strictEqual(ret, value22);
76+
expect(ret).toEqual(value22);
7977
});
8078
it('returns the correct start time if there are 22 values in /proc/[pid]/stat, including a trailing line '
8179
+ 'terminator', () => {
8280
let stat: string = createStatOutput('(bash)', 18);
8381
const value22: string = '12345';
8482
stat += ` ${value22}\n`;
8583
const ret: string|undefined = getProcessStartTimeFromProcStat(stat);
86-
assert.strictEqual(ret, value22);
84+
expect(ret).toEqual(value22);
8785
});
8886
it('returns the correct start time if the second value in /proc/[pid]/stat does not contain spaces', () => {
8987
let stat: string = createStatOutput('(bash)', 18);
9088
const value22: string = '12345';
9189
stat += ` ${value22}`;
9290
const ret: string|undefined = getProcessStartTimeFromProcStat(stat);
93-
assert.strictEqual(ret, value22);
91+
expect(ret).toEqual(value22);
9492
});
9593
});
9694

9795
if (process.platform === 'darwin' || process.platform === 'linux') {
9896
describe('Linux and Mac', () => {
9997
describe('getLockFilePath()', () => {
10098
it('returns a resolved path containing the pid', () => {
101-
assert.equal(
102-
path.join(process.cwd(), `test#${process.pid}.lock`),
99+
expect(
100+
path.join(process.cwd(), `test#${process.pid}.lock`)
101+
).toEqual(
103102
LockFile.getLockFilePath('./', 'test')
104103
);
105104
});
106105

107106
it('allows for overridden pid', () => {
108-
assert.equal(
109-
path.join(process.cwd(), `test#99.lock`),
107+
expect(
108+
path.join(process.cwd(), `test#99.lock`)
109+
).toEqual(
110110
LockFile.getLockFilePath('./', 'test', 99)
111111
);
112112
});
@@ -122,20 +122,20 @@ describe('LockFile', () => {
122122
const lock: LockFile | undefined = LockFile.tryAcquire(testFolder, resourceName);
123123

124124
// The lockfile should exist and be in a clean state
125-
assert.isDefined(lock);
126-
assert.isFalse(lock!.dirtyWhenAcquired);
127-
assert.isFalse(lock!.isReleased);
128-
assert.isTrue(FileSystem.exists(pidLockFileName));
125+
expect(lock).toBeDefined();
126+
expect(lock!.dirtyWhenAcquired).toEqual(false);
127+
expect(lock!.isReleased).toEqual(false);
128+
expect(FileSystem.exists(pidLockFileName)).toEqual(true);
129129

130130
// Ensure that we can release the "clean" lockfile
131131
lock!.release();
132-
assert.isFalse(FileSystem.exists(pidLockFileName));
133-
assert.isTrue(lock!.isReleased);
132+
expect(FileSystem.exists(pidLockFileName)).toEqual(false);
133+
expect(lock!.isReleased).toEqual(true);
134134

135135
// Ensure we cannot release the lockfile twice
136-
assert.throws(() => {
136+
expect(() => {
137137
lock!.release();
138-
});
138+
}).toThrow();
139139
});
140140

141141
it('cannot acquire a lock if another valid lock exists', () => {
@@ -166,23 +166,25 @@ describe('LockFile', () => {
166166
const lock: LockFile | undefined = LockFile.tryAcquire(testFolder, resourceName);
167167

168168
// this lock should be undefined since there is an existing lock
169-
assert.isUndefined(lock);
169+
expect(lock).toBeUndefined();
170170
});
171171
});
172172
}
173173

174174
if (process.platform === 'win32') {
175175
describe('getLockFilePath()', () => {
176176
it('returns a resolved path that doesn\'t contain', () => {
177-
assert.equal(
178-
path.join(process.cwd(), `test.lock`),
177+
expect(
178+
path.join(process.cwd(), `test.lock`)
179+
).toEqual(
179180
LockFile.getLockFilePath('./', 'test')
180181
);
181182
});
182183

183184
it('ignores pid that is passed in', () => {
184-
assert.equal(
185-
path.join(process.cwd(), `test.lock`),
185+
expect(
186+
path.join(process.cwd(), `test.lock`)
187+
).toEqual(
186188
LockFile.getLockFilePath('./', 'test', 99)
187189
);
188190
});
@@ -202,7 +204,7 @@ describe('LockFile', () => {
202204
const lock: LockFile | undefined = LockFile.tryAcquire(testFolder, resourceName);
203205

204206
// this lock should be undefined since there is an existing lock
205-
assert.isUndefined(lock);
207+
expect(lock).toBeUndefined();
206208
lockFileHandle.close();
207209
});
208210

@@ -219,15 +221,15 @@ describe('LockFile', () => {
219221

220222
const lock: LockFile | undefined = LockFile.tryAcquire(testFolder, resourceName);
221223

222-
assert.isDefined(lock);
223-
assert.isTrue(lock!.dirtyWhenAcquired);
224-
assert.isFalse(lock!.isReleased);
225-
assert.isTrue(FileSystem.exists(lockFileName));
224+
expect(lock).toBeDefined();
225+
expect(lock!.dirtyWhenAcquired).toEqual(true);
226+
expect(lock!.isReleased).toEqual(false);
227+
expect(FileSystem.exists(lockFileName)).toEqual(true);
226228

227229
// Ensure that we can release the "dirty" lockfile
228230
lock!.release();
229-
assert.isFalse(FileSystem.exists(lockFileName));
230-
assert.isTrue(lock!.isReleased);
231+
expect(FileSystem.exists(lockFileName)).toEqual(false);
232+
expect(lock!.isReleased).toEqual(true);
231233
});
232234

233235
it('can acquire and close a clean lockfile', () => {
@@ -241,20 +243,20 @@ describe('LockFile', () => {
241243
const lock: LockFile | undefined = LockFile.tryAcquire(testFolder, resourceName);
242244

243245
// The lockfile should exist and be in a clean state
244-
assert.isDefined(lock);
245-
assert.isFalse(lock!.dirtyWhenAcquired);
246-
assert.isFalse(lock!.isReleased);
247-
assert.isTrue(FileSystem.exists(lockFileName));
246+
expect(lock).toBeDefined();
247+
expect(lock!.dirtyWhenAcquired).toEqual(false);
248+
expect(lock!.isReleased).toEqual(false);
249+
expect(FileSystem.exists(lockFileName)).toEqual(true);
248250

249251
// Ensure that we can release the "clean" lockfile
250252
lock!.release();
251-
assert.isFalse(FileSystem.exists(lockFileName));
252-
assert.isTrue(lock!.isReleased);
253+
expect(FileSystem.exists(lockFileName)).toEqual(false);
254+
expect(lock!.isReleased).toEqual(true);
253255

254256
// Ensure we cannot release the lockfile twice
255-
assert.throws(() => {
257+
expect(() => {
256258
lock!.release();
257-
});
259+
}).toThrow();
258260
});
259261
}
260262
});
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
/// <reference types="mocha" />
5-
/* tslint:disable:no-function-expression - Mocha uses a poorly scoped "this" pointer */
6-
7-
import { assert } from 'chai';
84
import * as path from 'path';
95
import { PackageJsonLookup } from '../PackageJsonLookup';
106
import { IPackageJson } from '../IPackageJson';
117

12-
describe('PackageJsonLookup', function (): void {
8+
describe('PackageJsonLookup', () => {
139

14-
describe('basic tests', function (): void {
10+
describe('basic tests', () => {
1511

16-
it('tryLoadPackageJsonFor() test', function (): void {
12+
it('tryLoadPackageJsonFor() test', () => {
1713
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
1814
const sourceFilePath: string = path.join(__dirname, './test-data/example-package');
1915
const packageJson: IPackageJson | undefined = packageJsonLookup.tryLoadPackageJsonFor(sourceFilePath);
20-
assert.ok(packageJson);
16+
expect(packageJson).toBeDefined();
2117
if (packageJson) {
22-
assert.equal(packageJson.name, 'example-package');
23-
assert.equal(packageJson.version, '1.0.0');
18+
expect(packageJson.name).toEqual('example-package');
19+
expect(packageJson.version).toEqual('1.0.0');
2420

2521
// The "nonstandardField" should have been trimmed because loadExtraFields=false
26-
// tslint:disable-next-line:no-string-literal
27-
assert.notOk(packageJson['nonstandardField']);
22+
expect(packageJson).not.toHaveProperty('nonstandardField');
2823
}
2924
});
3025

31-
it('tryGetPackageFolderFor() test', function (): void {
26+
it('tryGetPackageFolderFor() test', () => {
3227
const packageJsonLookup: PackageJsonLookup = new PackageJsonLookup();
3328
const sourceFilePath: string = path.join(__dirname, './test-data/example-package/src/ExampleFile.txt');
3429

3530
// Example: C:\web-build-tools\libraries\node-core-library\src\test\example-package
3631
const foundFolder: string | undefined = packageJsonLookup.tryGetPackageFolderFor(sourceFilePath);
37-
assert.isTrue(foundFolder && foundFolder.search(/[\\/]example-package$/i) >= 0,
38-
'Unexpected result: ' + foundFolder);
32+
expect(foundFolder).toBeDefined();
33+
expect(foundFolder!.search(/[\\/]example-package$/i)).toBeGreaterThan(0);
3934

4035
const foundFile: string | undefined = packageJsonLookup.tryGetPackageJsonFilePathFor(sourceFilePath);
4136

42-
assert.equal(foundFile, path.join(foundFolder || '', 'package.json'));
37+
expect(foundFile).toEqual(path.join(foundFolder || '', 'package.json'));
4338
});
4439
});
4540
});

0 commit comments

Comments
 (0)