Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
trusktr opened this issue Feb 27, 2019 · 5 comments
Closed

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

trusktr opened this issue Feb 27, 2019 · 5 comments
Labels

Comments

@trusktr
Copy link

trusktr commented Feb 27, 2019

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
Copy link
Owner

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
Copy link
Author

trusktr commented Apr 9, 2019

@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
Copy link
Author

trusktr commented Apr 9, 2019

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

@twolfson
Copy link
Owner

twolfson commented Apr 9, 2019 via email

@trusktr
Copy link
Author

trusktr commented Apr 12, 2019

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

karma.config.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
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants