Error: Uncaught [ReferenceError: require is not defined]
boris-meerovich-sp opened this issue · comments
I encountered the error ('require is not defined') when tried to run some simple test.
My environment:
- windows 10 64 bit
- node v6.9.4
- npm 3.10.10
packages.json:
- "jasmine": "^2.8.0",
- "jasmine-core": "^2.6.0",
- "jasmine-spec-reporter": "^2.4.0",
- "jsdom": "^11.5.1",
- "jsdom-global": "^3.0.2",
- "karma": "^1.7.1",
- "karma-jasmine": "^1.1.0",
- "karma-jsdom-launcher": "^6.1.2",
- "karma-spec-reporter": "0.0.31"
karma.config:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'e2e-jsdom/components/**/*.e2e.js'
],
exclude: [],
port: 8090,
logLevel: config.LOG_INFO,
reporters: ['spec'],
specReporter: {
maxLogLines: 5, // limit number of lines logged per test
suppressErrorSummary: false, // do not print error summary
suppressFailed: false, // do not print information about failed tests
suppressPassed: false, // do not print information about passed tests
suppressSkipped: true, // do not print information about skipped tests
showSpecTiming: true, // print the time elapsed for each spec
failFast: false // test would finish with error when a first fail occurs.
},
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
browsers: ['jsdom'],
jsdomLauncher: {
jsdom: {
// url: "http://localhost:9000/",
//referrer: "http://localhost:9000",
//contentType: "text/html",
userAgent: "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
includeNodeLocations: true
}
},
singleRun: false
});
};
spec file:
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
describe('spec', () => {
let dom;
beforeEach(() => {
dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
});
it('should ...', () => {
expect(dom.window.document.querySelector("p").textContent).toBe("Hello world");
});
});
When I run it with karma (karma start), I got the following error:
27 11 2017 10:34:49.311:WARN [karma]: No captured browser, open http://localhost:8090/
27 11 2017 10:34:49.325:WARN [karma]: Port 8090 in use
27 11 2017 10:34:49.327:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:8091/
27 11 2017 10:34:49.327:INFO [launcher]: Launching browser jsdom with unlimited concurrency
27 11 2017 10:34:49.372:INFO [launcher]: Starting browser jsdom
27 11 2017 10:34:49.647:INFO [Chrome 41.0.2228 (Windows 7 0.0.0)]: Connected on socket dpd9CpdH082-qWKbAAAA with id 31834240
Error: Uncaught [ReferenceError: require is not defined]
at reportException (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\helpers\runtime-script-errors.js:66:24)
at processJavaScript (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:142:7)
at HTMLScriptElementImpl._eval (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:65:7)
at e (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:31:22)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:91:11)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:94:23)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:94:23)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:94:23)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:94:23)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:94:23)
at C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:110:12
at wrappedEnqueued (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:255:16)
at Request.request [as _callback] (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:203:9)
at Request.self.callback (C:\Projects\POCs\jsdom-poc\node_modules\request\request.js:186:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7) ReferenceError: require is not defined
at http://localhost:8091/base/e2e-jsdom/components/test-comp.e2e.js?0c6123dc1da3c41b938e13b2d1d7ee71298f5ec8:1:15
at ContextifyScript.Script.runInContext (vm.js:35:29)
at Object.exports.runInContext (vm.js:67:17)
at processJavaScript (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:140:10)
at HTMLScriptElementImpl._eval (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:65:7)
at e (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:31:22)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:91:11)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:94:23)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:94:23)
at Object.check (C:\Projects\POCs\jsdom-poc\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:94:23)
Chrome 41.0.2228 (Windows 7 0.0.0) ERROR
require is not defined
at http://localhost:8091e2e-jsdom/components/test-comp.e2e.js:1
Chrome 41.0.2228 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.114 secs / 0 secs)
When I run it with Jasmine itself on nodeJS, the test is passed.
Looks I missed something...
Please, advise.
Thanks
When using jsdom with this library, you don't have to initialize a browser object, because your tests will already be running within the context of such. In other words, karma-jsdom-launcher abstracts this away from you and allow you to run the same tests in other browsers as well.
If you only intend to target jsdom and prefer to initialize the browser object yourself, then you don't actually have to use Karma at all.