Skip to content

Commit

Permalink
test(node-unit/mocha.spec.js): Refactor and beef up tests
Browse files Browse the repository at this point in the history
Migrated `Mocha#addFiles` here. Created `Mocha#loadFiles`. Added chainable tests where applicable.
Refactored.
  • Loading branch information
plroebuck committed Feb 14, 2019
1 parent 96694e4 commit 43ec922
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions test/node-unit/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,46 @@ 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('.unloadFile()', function() {
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);

Expand All @@ -19,23 +54,19 @@ describe('Mocha', function() {
});
});

describe('.unloadFiles()', function() {
describe('#unloadFiles', function() {
it('should unload all test files from cache', function() {
const mocha = new Mocha(opts);
let resolvedTestFiles;
const testFiles = [
__filename,
path.join(__dirname, 'cli', 'config.spec.js'),
path.join(__dirname, 'cli', 'run.spec.js')
];

testFiles.forEach(mocha.addFile, mocha);
mocha.loadFiles();
resolvedTestFiles = testFiles.map(require.resolve);
expect(require.cache, 'to have keys', resolvedTestFiles);

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);
});
});
});

0 comments on commit 43ec922

Please sign in to comment.