twolfson / karma-electron

Karma launcher and preprocessor for Electron
The Unlicense
59 stars 21 forks source link

Errors without stack traces coming from karma-electron? #35

Closed trusktr closed 5 years ago

trusktr commented 5 years ago

When I get errors in my test code, I often see them formatted in the console like so:

Electron 2.0.17 (Node 8.9.3) ERROR
  {
    "message": "Uncaught TypeError: /Users/joepea/src/trusktr+infamous/src/html/behaviors/Behavior.js: Duplicate declaration \"whenDefined\"
(This is an error on an internal node. Probably an internal error.)\nat /Users/joepea/src/trusktr+infamous/node_modules/@babel/traverse/lib/s
cope/index.js:343:7\n\nundefined",
    "str": "Uncaught TypeError: /Users/joepea/src/trusktr+infamous/src/html/behaviors/Behavior.js: Duplicate declaration \"whenDefined\" (Thi
s is an error on an internal node. Probably an internal error.)\nat /Users/joepea/src/trusktr+infamous/node_modules/@babel/traverse/lib/scope
/index.js:343:7\n\nundefined"
  }

I noticed that the errors are the same format regardless which karma reporter I use.

Is it karma-electron outputting them like that? If so, how can I get it to output full formatted stack traces

twolfson commented 5 years ago

Looking at our resolution in #32, I'm going to assume we're still using babel-register. I have a feeling that there is no stack trace because there is no stack to dump (i.e. it's only 1 level deep)

The error looks like it's coming from the Babel transpiler reading in /Users/joepea/src/trusktr+infamous/src/html/behaviors/Behavior.js, it's not an error from Electron running the file -- but when it transpiles it instead

For reference, here's what a normal runtime error stacktrace looks like:

Electron 1.8.8 (Node 8.2.1) module for `require` based Node.js integrations in the top level identify as the page itself FAILED
    Error: Test error
        at throwTestError (/home/todd/github/karma-electron/test/integration-test/node-require-test.js:22:9)
        at Context.<anonymous> (/home/todd/github/karma-electron/test/integration-test/node-require-test.js:29:7)
trusktr commented 5 years ago

@twolfson Alright, I've done some debugging. I've found out that the errors are being thrown from within the require() calls that I have in the @babel/register setup.

However, the stack traces are deep, and I can see the stack traces if I modify the code to catch them and log them manually.

Here's that part of my karma config.

I modified that part of my karma config so it looks like the following:

testFiles.forEach(file => {
    const relativeFile = file.replace(CWD, '')
    const relativePath = dirname(relativeFile)

    mkdirp.sync( CWD + '/.karma-test-build' + relativePath )

    const nodeModulesToCompile = config.nodeModulesToCompile

    fs.writeFileSync( CWD + '/.karma-test-build' + relativeFile, `
        // NOTE, we don't use babel.config.js settings here, we can target a
        // more modern environment.
        require('@babel/register')({
            presets: [ ['@babel/preset-env', { targets: { node: 9 } }] ],
            plugins: [
                // We need to transpile the not-yet-official re-export syntax.
                '@babel/plugin-proposal-export-namespace-from',
                '@babel/plugin-proposal-export-default-from',
            ],
            sourceMap: 'inline',
            ${config.nodeModulesToCompile ? `
                ignore: [
                    // don't compile node_modules except for ones specified in the config
                    ${nodeModulesToCompile.map(moduleName => {
                        return r`/node_modules(?!\/${r.escape(moduleName)}\/)/`
                    })}
                ],
            ` : ''}
        })

        // RIGHT HERE, LOG THE ERROR ----------------------------
        try {
            require( '${ file }' )
        } catch(e) {
            console.log(e)
            process.exit()
        }
    ` )
})

Before doing this, I was seeing an error like:

Electron 2.0.18 (Node 8.9.3) ERROR
  {
    "message": "Uncaught TypeError: Cannot read property 'default' of undefined\nat /Users/trusktr/src/trusktr+infamous/src/core/ImperativeBase.js:100:129\n\nundefined",
    "str": "Uncaught TypeError: Cannot read property 'default' of undefined\nat /Users/trusktr/src/trusktr+infamous/src/core/ImperativeBase.js:100:129\n\nundefined"
  }

Then after adding the try-catch and logging the error I was able to see:

Electron 2.0.18 (Node 8.9.3) LOG: TypeError: Cannot read property 'default' of undefined
TypeError: Cannot read property 'default' of undefined
    at exports.default.Base (/Users/trusktr/src/trusktr+infamous/src/core/ImperativeBase.js:100:129)
    at /Users/trusktr/src/trusktr+infamous/node_modules/lowclass/Mixin.js:54:36
    at /Users/trusktr/src/trusktr+infamous/node_modules/lowclass/Mixin.js:64:17
    at /Users/trusktr/src/trusktr+infamous/node_modules/lowclass/Mixin.js:132:17
    at /Users/trusktr/src/trusktr+infamous/node_modules/lowclass/Mixin.js:44:12
    at ApplyDefault (/Users/trusktr/src/trusktr+infamous/node_modules/lowclass/Mixin.js:119:22)
    at Mixin (/Users/trusktr/src/trusktr+infamous/node_modules/lowclass/Mixin.js:37:13)
    at initImperativeBase (/Users/trusktr/src/trusktr+infamous/src/core/ImperativeBase.js:100:58)
    at getImperativeBaseProtectedHelper (/Users/trusktr/src/trusktr+infamous/src/core/ImperativeBase.js:62:3)
    at Object.<anonymous> (/Users/trusktr/src/trusktr+infamous/src/core/Motor.js:15:86)

So my question is, if I don't catch the error and log it my self, what in the stack is deciding to display such a limited version of the error?

trusktr commented 5 years ago

Seems like something in karma or karma-electron is choosing not to log the full error output?

twolfson commented 5 years ago

I can't think of anything off the top of my head. Pretty confident it's either Karma or your repo since we do nothing to tweak errors in karma-electron

Easiest way to find out would be to try another Karma engine (would disqualify/prove karma-electron) and use a simpler repo (would disqualify/prove your repo)

On Tue, Apr 9, 2019, 2:57 PM Joe Pea notifications@github.com wrote:

Seems like something in karma or karma-electron is choosing not to log the full error output?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/twolfson/karma-electron/issues/35#issuecomment-481455301, or mute the thread https://github.com/notifications/unsubscribe-auth/AA3FWDkER8iZFZLANlBqLgfGPexmUk1Lks5vfQzJgaJpZM4bT8NW .

trusktr commented 5 years ago

Alright, after disabling karma-electron, and using default values for the browser,

karma.config.js ```js module.exports = function(config) { config.set({ frameworks: ['jasmine', 'stacktrace'], port: 9876, // karma web server port colors: true, logLevel: config.LOG_INFO, autoWatch: false, singleRun: debugMode ? false : true, concurrency: Infinity, basePath: CWD, files: [ 'tests/**/*.js', ], client: { useIframe: false, loadScriptsViaRequire: true, }, }) } ```

the errors are still in the same format. So for this simple test file,

test file ```js console.log( 'test.js' ) throw new Error('test error') describe('Tests...', () => { it('needs to be implemented', () => { expect(true).toBe(true) }) }) ```

the error output is in the same format, but they are now tagged with Chrome instead of Electron:

error output: ``` 09 04 2019 19:26:37.163:INFO [karma-server]: Karma v4.0.1 server started at http://0.0.0.0:9876/ 09 04 2019 19:26:42.625:INFO [Chrome 73.0.3683 (Mac OS X 10.14.3)]: Connected on socket pI2-vVfZy7mtUSeIAAAA with id manual-6446 Chrome 73.0.3683 (Mac OS X 10.14.3) ERROR { "message": "An error was thrown in afterAll\nError: test error\n at tests/test.js:3:7", "str": "An error was thrown in afterAll\nError: test error\n at tests/test.js:3:7" } ```

Longer errors can be even worse:

longer error: ``` 09 04 2019 19:25:43.803:INFO [karma-server]: Karma v4.0.1 server started at http://0.0.0.0:9876/ 09 04 2019 19:25:46.689:INFO [Chrome 73.0.3683 (Mac OS X 10.14.3)]: Connected on socket EkGfojpmOhFDLuihAAAA with id manual-3879 Chrome 73.0.3683 (Mac OS X 10.14.3) ERROR: 'DEPRECATION:', 'Setting specFilter directly on Env is deprecated, please use the specFilter option in `configure`' Chrome 73.0.3683 (Mac OS X 10.14.3) ERROR { "message": "An error was thrown in afterAll\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\ nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token {\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected iden tifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntax Error: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpec ted identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier \nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxErro r: Unexpected token {\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token {\nSyntaxError: Unexpected identifier\nSynt axError: Unexpected identifier\nSyntaxError: Unexpected token {\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected string\nSyntaxError: Unexpected string\nSyntaxError: Unexpected strin g\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token {\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected string\nSyntaxError: Unexp ected identifier\nSyntaxError: Unexpected string\nSyntaxError: Unexpected string\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token export \nSyntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxEr ror: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpecte d identifier\nSyntaxError: Unexpected token *\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token *\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token *", "str": "An error was thrown in afterAll\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\nSyn taxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unex pected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token {\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifi er\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxErro r: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSy ntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: U nexpected token {\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected ide ntifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token {\nSyntaxError: Unexpected identifier\nSyntaxEr ror: Unexpected identifier\nSyntaxError: Unexpected token {\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected string\nSyntaxError: Unexpected string\nSyntaxError: Unexpected string\nS yntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token {\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected string\nSyntaxError: Unexpecte d identifier\nSyntaxError: Unexpected string\nSyntaxError: Unexpected string\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token export\nSy ntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected identifier\nSyntaxError: Unexpected id entifier\nSyntaxError: Unexpected token *\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token *\nSyntaxError: Unexpected token export\nSyntaxError: Unexpected token *" } ```

So it seems like it is something to do with Karma.