Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Test helper #169

Merged
merged 9 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/intern/internLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
declare const shimAmdDependencies: any;

intern.registerLoader((options) => {
return intern.loadScript('node_modules/@dojo/loader/loader.js')
.then(() => intern.loadScript('node_modules/@dojo/shim/util/amd.js'))
.then(() => {
(<any> require).config(shimAmdDependencies({
baseUrl: options.baseUrl || intern.config.basePath,
packages: [
{'name': 'sinon', 'location': 'node_modules/sinon/pkg', 'main': 'sinon'}
]
}));

// load @dojo/shim/main to import the ts helpers
return new Promise<void>((resolve) => {
(<any> require)(['@dojo/shim/main'], () => {
resolve();
});
});
}).then(() => {
return (modules: string[]) => {
return new Promise<void>((resolve, reject) => {
(<any> require)(modules, () => resolve());
});
};
});
});
10 changes: 9 additions & 1 deletion options/intern.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import * as path from 'path';

export = function (grunt: IGrunt) {
grunt.loadNpmTasks('intern');

const progress = grunt.option<boolean>('progress');

// read the test config and find a loader. if no loader is specified, we need to add ours
const { browser: { loader: browserLoader = undefined } = {}, loader } = grunt.file.readJSON(path.resolve(grunt.config.get('internConfig')));

return {
options: {
config: '<%= internConfig %>',
reporters: [
{ name: 'runner', options: { 'hideSkipped': !progress, 'hidePassed': !progress } }
]
],
browser: (loader || browserLoader) ? {} : {
loader: './node_modules/grunt-dojo2/lib/intern/internLoader.js'
}
},
browserstack: {
options: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@
"@types/lodash": "4.14.56",
"@types/mockery": "~1.4.29",
"@types/node": "^7.0.0",
"@types/sinon": "~1.16.32",
"@types/shelljs": "^0.6.0",
"@types/sinon": "~1.16.32",
"dojo-loader": "latest",
"grunt": "^1.0.1",
"istanbul-lib-instrument": "^1.9.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know that I technically need this as it is already loaded by intern...

"mockery": "^2.0.0",
"shx": ">=0.1.2",
"sinon": "^1.17.6",
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/all.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/unit/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ import './tasks/uploadCoverage';
import './tasks/util/process';
import './tasks/util/postcss';
import './tasks/util/Publisher';
import './options/intern';
import './lib/load-dojo-loader';
import './lib/internLoader';
139 changes: 139 additions & 0 deletions tests/unit/lib/internLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import * as vm from 'vm';
import { Context } from 'vm';
import * as fs from 'fs';
import * as path from 'path';
import * as sinon from 'sinon';
import { SinonSandbox } from 'sinon';
import * as istanbul from 'istanbul-lib-instrument';

const { registerSuite } = intern.getInterface('object');
const { assert } = intern.getPlugin('chai');

let sandbox: SinonSandbox;
let internMock: any;
let requireMock: any;
let context: Context;
let shimMock: any;
let loaderPromise: Promise<any> | undefined;

const instrumenter = istanbul.createInstrumenter({
esModules: true
});

const sourceFile = path.resolve(__dirname, '../../../lib/intern/internLoader.js');
const loaderCode = fs.readFileSync(sourceFile).toString();
const instrumentedCode = instrumenter.instrumentSync(loaderCode, sourceFile);

function reportCoverage() {
intern.emit('coverage', {
coverage: (<any> context)['__coverage__'],
source: '',
sessionId: intern.config.sessionId
});
}

function runTest(before: Function, after: Function) {
return new Promise((resolve, reject) => {
before();
vm.runInContext(instrumentedCode, context);

if (loaderPromise) {
loaderPromise.then((result) => {
after(result);
resolve();
});
} else {
setTimeout(() => {
reportCoverage();

after();
resolve();
}, 100);

}
});
}

registerSuite('lib/intern/internLoader', {
beforeEach() {
sandbox = sinon.sandbox.create();
requireMock = sandbox.stub().callsArg(1);
(<any> requireMock).config = sandbox.stub();

loaderPromise = undefined;

internMock = {
registerLoader: (cb: Function) => {
loaderPromise = cb({
baseUrl: '/options'
});
},
loadScript: sandbox.spy(() => Promise.resolve()),
config: {
basePath: '/config'
}
};

shimMock = sandbox.stub().returnsArg(0);

context = vm.createContext({
intern: internMock,
require: requireMock,
shimAmdDependencies: shimMock
});
},
afterEach() {
sandbox.restore();
},
tests: {
async 'registers the intern loader'() {
return runTest(() => {
sandbox.stub(internMock, 'registerLoader');
}, () => {
assert.isTrue(internMock.registerLoader.called);
});
},

async 'loads the AMD util'() {
return runTest(() => {
}, () => {
assert.isTrue(internMock.loadScript.calledWith('node_modules/@dojo/loader/loader.js'));
assert.isTrue(internMock.loadScript.calledWith('node_modules/@dojo/shim/util/amd.js'));
});
},

async 'configures the loader with the baseurl from options'() {
return runTest(() => {
}, () => {
assert.equal(requireMock.config.args[0][0].baseUrl, '/options');
});
},

async 'configures the loader with the baseurl from config'() {
return runTest(() => {
sandbox.stub(internMock, 'registerLoader').callsArgWith(0, {});
}, () => {
assert.equal(requireMock.config.args[0][0].baseUrl, '/config');
});
},

async 'loads shim/main'() {
return runTest(() => {
}, () => {
assert.equal(requireMock.args[0][0], '@dojo/shim/main');
});
},

async 'creates a loader that uses require'() {
return new Promise((resolve) => {
runTest(() => {
}, (result: any) => {
result(['some-module']).then(() => {
assert.isTrue(requireMock.calledWith(['some-module']));
resolve();
});
});
});
}
}
});
69 changes: 69 additions & 0 deletions tests/unit/options/intern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { registerSuite } = intern.getInterface('object');
const { assert } = intern.getPlugin('chai');

import * as path from 'path';
import * as grunt from 'grunt';
import { getInputDirectory } from '../util';

const configPath = path.resolve(getInputDirectory() + '/intern.json');

registerSuite('options/intern', {
before() {
grunt.initConfig({
internConfig: configPath
});
},
afterEach() {
delete require.cache[require.resolve('../../../options/intern')];
},
tests: {
'uses loader specified in the intern config root'() {
const config = require('../../../options/intern')({
...grunt,
loadNpmTasks() {
},
file: {
readJSON() {
return {
loader: 'test'
};
}
}
});

assert.isTrue(config.options.browser.loader === undefined);
},
'uses loader specified in the intern browser config'() {
const config = require('../../../options/intern')({
...grunt,
loadNpmTasks() {
},
file: {
readJSON() {
return {
browser: {
loader: 'test'
}
};
}
}
});

assert.isTrue(config.options.browser.loader === undefined);
},
'injects loader if its empty'() {
const config = require('../../../options/intern')({
...grunt,
loadNpmTasks() {
},
file: {
readJSON() {
return {};
}
}
});

assert.isTrue(config.options.browser.loader === './node_modules/grunt-dojo2/lib/intern/internLoader.js');
}
}
});