mfncooper / mockery

Simplifying the use of mocks with Node.js
Other
1.1k stars 60 forks source link

Unexpected token import #55

Closed ryan-nauman closed 7 years ago

ryan-nauman commented 7 years ago

I have no idea what the source of this error is. I'm hoping you can at least point me in the right direction.

import { expect } from 'chai';
import * as mockery from 'mockery';
import * as sinon from 'sinon';
import Spotify from '../src/lib/Spotify';
import ActionTypes from '../src/lib/ActionTypes';

describe('Player', () => {

  let player, spotifyStub;

  before(() => {
    mockery.enable({
      warnOnReplace: true,
      warnOnUnregistered: true,
      useCleanCache: true
    });

    spotifyStub = sinon.createStubInstance(Spotify);
    spotifyStub.emit.restore();

    // replace the module `Spotify` with a stub object
    mockery.registerMock('./Spotify', spotifyStub);

    debugger;

    player = require('../src/lib/Player'); // errors
  });

  after(() => {
    mockery.disable();
  });

  it('should retry connection on spotify error', (done) => {
    ...
  })

});

Errors with:

1) Player "before all" hook:
   xxx/src/lib/player.js:1
(function (exports, require, module, __filename, __dirname, process, global) { import Spotify from './Spotify';
                                                                             ^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Module._load (module.js:409:3)
    at Function.hookedLoader [as _load] (xxx/node_modules/mockery/mockery.js:111:12)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Context.before (xxx/app/spec/player.spec.ts:26:14)
    at runCallback (timers.js:566:20)
    at tryOnImmediate (timers.js:546:5)
    at processImmediate [as _immediateCallback] (timers.js:525:5)

Where Player.js starts with an import...

import Spotify from './Spotify';
class Player {
...
ryan-nauman commented 7 years ago

The source of this error was how I am compiling to ES6