This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Test helper #169
Merged
Merged
Test helper #169
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c48bb27
Automatically injecting intern loader
rorticus 5ef81d3
Adding ability to override loader
rorticus 64e3c18
Optionally adding loader so that intern config can override it comple…
rorticus 5f0c8ac
Excluding loader from coverage
rorticus c3a9960
Looking in the loader config as well as browser.loader
rorticus 30ab2eb
Adding tests for intern options
rorticus 188ec00
Adding tests
rorticus 8054d26
Adding tests for intern loader
rorticus d8ce2f8
Removing js file
rorticus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...