sorry-cypress / sorry-cypress

Open-source, free, self-hosted alternative to Cypress Dashboard
https://sorry-cypress.dev
MIT License
2.7k stars 295 forks source link

First test randomly errors on thread when running: “Invalid or unexpected token” #40

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hello, this project is great and works great most of the time! We run our tests on a Codebuild on AWS and run 5 threads of the Cypress runner parallel with the director service on AWS. About a quarter of the time, the first test on one of the threads fails with this error:

An uncaught error was detected outside of a test
Invalid or unexpected token
This error originated from your test code, not from Cypress.
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test. We dynamically generated a new test to display this failure.

Now I'm not sure if this a sorry cypress issue or not, but I didn't have an issue with it before switching to running in parallel. If you don't think this is a sorry cypress issue, I can close this issue.

Tried many things to try to fix this, including setting modifyObtrusiveCode to false, chromeWebSecurity to false, upgrading Cypress. We are already catching uncaught exceptions so that doesn't seem like it should be the issue. I turned on some extra logs for this and here is the output

[3] 2020-03-06T19:57:20.369Z cypress:server:project onMocha start
[3] 2020-03-06T19:57:20.369Z cypress:server:reporter got mocha event 'start' with args: [ { start: '2020-03-06T19:57:20.366Z' } ]
[3] 2020-03-06T19:57:20.374Z cypress:server:project onMocha suite
[3] 2020-03-06T19:57:20.374Z cypress:server:reporter got mocha event 'suite' with args: [ { id: 'r1', title: '', root: true, type: 'suite', file: 'cypress/integration/ci-tests/content-acquisition/channels/channel-manual-upload-run-acquired-items-tab.spec.ts' } ]
[3]
[3] 2020-03-06T19:57:20.390Z cypress:server:project onMocha test
[3] 2020-03-06T19:57:20.391Z cypress:server:reporter got mocha event 'test' with args: [ { id: 'r2', title: 'An uncaught error was detected outside of a test', body: 'function throwErr() {\n throw err;\n }', type: 'test' } ]

[3] 2020-03-06T19:57:20.555Z cypress:server:reporter got mocha event 'fail' with args: [ { id: 'r2', title: 'An uncaught error was detected outside of a test', err: { message: 'Unexpected end of input\n' + '\n' + 'This error originated from your test code, not from Cypress.\n' + '\n' + 'When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.\n' + '\n' + 'Cypress could not associate this error to any specific test.\n' + '\n' + 'We dynamically generated a new test to display this failure.', name: 'Uncaught SyntaxError', stack: 'Uncaught SyntaxError: Unexpected end of input\n' + '\n' + 'This error originated from your test code, not from Cypress.\n' + '\n' + 'When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.\n' + '\n' + 'Cypress could not associate this error to any specific test.\n' + '\n' + 'We dynamically generated a new test to display this failure.' }, state: 'failed', body: 'function throwErr() {\n      throw err;\n    }', type: 'test', duration: 179, wallClockStartedAt: '2020-03-06T19:57:20.374Z', timings: { lifecycle: 26, test: [Object] } } ]

I couldn't really make anything of these errors, but maybe someone else can. I'm kind of out of ideas on what to try (I've tried more things today than I've listed but can't recall them all). Any ideas?

agoldis commented 4 years ago

@tallkid24 thanks for kind words!

Let's try isolate the issue

As you've mentioned it is unlikely that sorry cypress is to blame - it has no effect on tests themselves, it just tells cypress agents what spec file to run

KyleThen commented 4 years ago

@agoldis I actually think it might be because all of the cypress runs are running in the same codebuild directory, so the transpiling process is overwriting what a different thread of the cypress run is creating.

I did see a different error come up on the dashboard: Error: GraphQL error: Cannot return null for non-nullable field InstanceResults.tests.

agoldis commented 4 years ago

@KyleThenTR good suggestion, I have extracted your comment https://github.com/agoldis/sorry-cypress/issues/40

KyleThen commented 4 years ago

@agoldis you were correct that it was an issue with transpiling and running all of the cypress threads on the same build machine. They were transpiling a file, and then the next runner would also transpile the same file, and the first test would pick up an incomplete file most likely.

For anyone else who comes across this issue, here is what I added in my plugins/index.js file.

    const runUuid = uuid.v4();
    on("file:preprocessor", (file) => {
        if (config.env.createTranspileUuids) {
            file.outputPath = file.outputPath.replace(/^(.*\/)(.*?)(\..*)$/, `$1$2.${runUuid}$3`);
        }
        return cypressTypeScriptPreprocessor(file);
    });

This issue can be closed.

TenPotatoes commented 2 years ago

Interesting. I was having this issue with cypress-parallel + cucumber instead of sorry-cypress + typescript, fix seems to work.

    function uuid() {
        return Date.now().toString(36) + Math.random().toString(36).substring(2);
    }

    on('file:preprocessor', (file) => {
        const runUuid = uuid();
        if (config.env.createTranspileUuids) {
            file.outputPath = file.outputPath.replace(/^(.*\/)(.*?)(\..*)$/, `$1$2.${runUuid}$3`);
        }
        return cucumber()(file);
    });
varshanharshank commented 1 year ago

I tried it below code in cypress 12.11 and it is not working

on('file:preprocessor', (file) => { const id = uuid() file.outputPath = file.outputPath.replace( /^(.\/)(.?)(..*)$/, $1$2.${id}$3 ) return filePreprocessor(file) }) on( 'file:preprocessor', browserify(config, { typescript: require.resolve('typescript'), }) )

DanielCambray commented 1 year ago

I think I am the same issue. I have sorry cypress + cucumber + typescript. In my config flie I have :

on(
  'file:preprocessor',
  createBundler({
    plugins: [createEsbuildPlugin(config)],
  })
)

Cypress is running fine with one thread. If I try to launch several of them multiple times I have bunch of errors :

[2] 1) An uncaught error was detected outside of a test: [2] SyntaxError: The following error originated from your test code, not from Cypress. [2] [2] > Unexpected end of input

varshanharshank commented 1 year ago

@TenPotatoes help out to resolve the issue