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

SyntaxError: Unexpected token export when importing lux model in jest test #695

Open
jamemackson opened this issue May 9, 2017 · 4 comments
Assignees

Comments

@jamemackson
Copy link
Contributor

I am having issues getting tests run run in jest that import a lux module but I can import other node modules. I setup a barebones lux application that shows this error:

https://github.com/jamemackson/lux-test-error-importing-model

On master I'm getting errors on import and export:

npm test

> lux-test-error-importing-model@0.0.1 test /work/tmp/test-lux/lux-test-error-importing-model
> NODE_ENV=test jest

 FAIL  app/utils/model-to-pojo.test.js
  ● Test suite failed to run

    /work/tmp/test-lux/lux-test-error-importing-model/app/utils/model-to-pojo.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import User from '../models/user';
                                                                                             ^^^^^^
    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:290:17)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:106:13)

 FAIL  app/utils/token.test.js
  ● Test suite failed to run

    /work/tmp/test-lux/lux-test-error-importing-model/app/utils/token.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { getNewToken } from './token';
                                                                                             ^^^^^^
    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:290:17)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:106:13)

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        0.828s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

and on the babel-preset-env branch only export is throwing errors (diff):

npm test

> lux-test-error-importing-model@0.0.1 test /work/tmp/test-lux/lux-test-error-importing-model
> NODE_ENV=test jest

 FAIL  app/utils/model-to-pojo.test.js
  ● Test suite failed to run

    /work/tmp/test-lux/lux-test-error-importing-model/node_modules/lux-framework/src/index.js:2
    export { Model } from './packages/database';
    ^^^^^^
    SyntaxError: Unexpected token export

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:290:17)
      at Object.<anonymous> (app/models/user.js:1:185)
      at Object.<anonymous> (app/utils/model-to-pojo.test.js:1:115)

 PASS  app/utils/token.test.js
  test token utility
    ✓ can generate a new token (11ms)
    ✓ breaks when no data provided. (3ms)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        1.601s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

the offending test is the same in both branches:

import User from '../models/user';

import {
  default as modelToPojo
} from './modelsToPojo';

test('module exports functions', () => {
  expect(typeof modelToPojo === 'function').toBeTruthy();
});

test('can convert 1 model', () => {
  const userData = {
    firstName: 'jame',
    lastName: 'mackson'
  };
  const user = User.create(userData);

  expect(modelToPojo(user)).toMatchObject(userData);
});
  • uname -a: Darwin WEDMAC201505.domain 16.5.0 Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64
  • Platform: OS X
  • Database: sqllite
  • Lux Version: 1.1.7
  • Node Version: 7.6 - 7.10
@jamemackson jamemackson changed the title `SyntaxError: Unexpected token export SyntaxError: Unexpected token export when importing lux model in jest test May 9, 2017
@zacharygolba zacharygolba self-assigned this May 11, 2017
@zacharygolba
Copy link
Contributor

@jamemackson

The reason why this is occurring is because Lux is not prebuilt when it is published (by design). This is because the core Lux API is bundled with applications via the Lux CLI.

A quick hack to get around this bug would be to run lux build before you run your tests and then import your modules in your tests from the bundle directly.

Example:

import { UsersController } from '../../dist/bundle';

test('it has a name', async () => {
  expect(UsersController).toHaveProperty('name', 'UsersController');
});

For a long term solution, we may have to wait until #681 lands and a public testing api is exposed. That PR contains features like the ability to call your application as a single function which prevents you from having to actually spin up a Lux server for each test.

@jamemackson
Copy link
Contributor Author

Thanks for the advice @zacharygolba - I'll give this a go in the morning but this sounds promising 👍

Being able to call the application from a single function would be super rad as that will, beyond just being the proper way to do things, allow jest to calculate proper coverage but I have worked out a hack to serve the application in a separate process allowing calling the api from a test. That hack does require that I use --runInBand so the tests run serially and not in parallel but it works so i'll take it for now :) it's not all that elegant but gets the job done.

@jamemackson
Copy link
Contributor Author

@zacharygolba I've tried this out and it seems to be working. I am at least getting past this error now anyways! Now, I am now getting some new errors but I don't believe they are related to lux itself in any way... I think these are issues with 3rd party libraries that might not be playing nicely with being bundled or something. Thanks again for the advice!

@jamemackson
Copy link
Contributor Author

@zacharygolba

so I've been tinkering around with this today, back in my isolated test app, and now I've run into a tangential issue. (I can start a new issue if you prefer but this kind of seems like an extension of this one so I figured I'd just tack it on here...)

Anyways, I've made some updates to my test app for this and am now getting this error:

TypeError: Cannot match against 'undefined' or 'null'. when I call const user = await User.create({...});

For me this has typically always been an issue with how I've setup relationships but this test app only has a user model and no relationships. I've also tested that I can create a user outside of the test and that works fine (testing w/ lux db:seed also) and I also even tried creating another model related to the user and that didn't change anything (I didn't commit those changes though...)

might you have any ideas for me here?

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

No branches or pull requests

2 participants