/
mocha.spec.js
72 lines (60 loc) · 2.11 KB
/
mocha.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const path = require('path');
const Mocha = require('../../lib/mocha');
const utils = require('../../lib/utils');
describe('Mocha', function() {
const opts = {reporter: utils.noop}; // no output
const testFiles = [
__filename,
path.join(__dirname, 'cli', 'config.spec.js'),
path.join(__dirname, 'cli', 'run.spec.js')
];
const resolvedTestFiles = testFiles.map(require.resolve);
describe('#addFile', function() {
it('should add the given file to the files array', function() {
const mocha = new Mocha(opts);
mocha.addFile(__filename);
expect(mocha.files, 'to have length', 1).and('to contain', __filename);
});
it('should be chainable', function() {
const mocha = new Mocha(opts);
expect(mocha.addFile(__filename), 'to be', mocha);
});
});
describe('#loadFiles', function() {
it('should load all files from the files array', function() {
const mocha = new Mocha(opts);
testFiles.forEach(mocha.addFile, mocha);
mocha.loadFiles();
expect(require.cache, 'to have keys', resolvedTestFiles);
});
it('should execute the optional callback if given', function() {
const mocha = new Mocha(opts);
expect(cb => {
mocha.loadFiles(cb);
}, 'to call the callback');
});
});
describe('.unloadFile', function() {
it('should unload a specific file from cache', function() {
const resolvedFilePath = require.resolve(__filename);
require(__filename);
expect(require.cache, 'to have key', resolvedFilePath);
Mocha.unloadFile(__filename);
expect(require.cache, 'not to have key', resolvedFilePath);
});
});
describe('#unloadFiles', function() {
it('should unload all test files from cache', function() {
const mocha = new Mocha(opts);
testFiles.forEach(mocha.addFile, mocha);
mocha.loadFiles();
mocha.unloadFiles();
expect(require.cache, 'not to have keys', resolvedTestFiles);
});
it('should be chainable', function() {
const mocha = new Mocha(opts);
expect(mocha.unloadFiles(), 'to be', mocha);
});
});
});