9

Does anyone know the correct way to set up testing with ts-node in typescript to have Mocha and Chai and Sinon all recognized globally without the need to manually import in every test file? I have all types definitions included in the project, including mocha, sinon, chai. I have the following (which works and runs tests correctly):

mocha.opts:

--require ts-node/register
--option value spec/**/*.spec.ts
--require spec/bootstrap/test-setup.js
--recursive
--retries 0
--sort
--timeout 10000
--ui bdd

Test script in package.json:

"test": "yarn build && NODE_ENV=test nyc mocha ./spec/bootstrap/test-helper.js --opts ./spec/mocha.opts"

I have 2 helper methods (which for some reason I have to do in JS)

test-setup.js:

require('mocha');
var chai = require('chai');
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));

global.expect = chai.expect;
global.Promise = require('bluebird');

test-helper.js:

const sinon = require('sinon');
const _ = require('lodash');
const db = require('../../src/models');

before(() => {
  global.sandbox = sinon.sandbox.create();
});

afterEach(function () {
  global.sandbox.restore();
});

after(() => {
  db.default.sequelize.close();
});

Finally a junk spec for testing setup:

describe('app', () => {
  const bar = {
    foo: () => 1,
  };

  it('asserts 1 === 1', () => {
    const stub = sandbox.stub(bar, 'foo').returns(3);
    const result = bar.foo();
    expect(result).to.eq(3);
    expect(stub).to.have.callCount(1);
    expect(1).to.eq(1);
  });

  it('tests stubs reset', () => {
    expect(bar.foo()).to.eq(1);
  });

});

A couple notes here:

  1. This works, it transpiles and runs the tests correctly. If i remove the seemingly pointless require('mocha') it shows error highlighting for it and describe as well, but again still runs.
  2. The only "issue" is the error highlighting and lack of auto complete for sinon and chai stuff in vs code. This is very close to my test setup in node js, which works 100% fine.

Here is what I see: Faulty error highlighting

But again I stress, this works, it just feels dubiously hacky and I would like to figure out if I can A) do this all in TS, rather than having JS setup files, and B) fix incorrect ts error highlighting in this scenario.

pjo336
  • 141
  • 2
  • 8
  • 1
    For the error highlighting issue, try `npm install -D @types/mocha @types/sinon @types/sinon-chai`. Not sure about the JS setup; perhaps try [ts-mocha](https://www.npmjs.com/package/ts-mocha). – tony19 Feb 16 '18 at 04:51
  • @tony19 Whoops definitely should have mentioned I already have those included. Thanks for pointing out – pjo336 Feb 16 '18 at 05:19
  • Hmm. Do you have a link to a GitHub repo? – tony19 Feb 16 '18 at 05:26
  • @tony19 Threw up this really quick: https://github.com/pjo336/test-ts – pjo336 Feb 16 '18 at 05:48
  • 1
    I'm having a similar issue, but only when i try to debug a test. It runs and I see no red squiggly lines when I open a test file, but when I try to debug a test written in typescript it breaks on `describe` or `it` as it can't find some of the symbols. – MilkyWayJoe Feb 23 '18 at 21:11
  • I had this exact issue. To replicate, use a regular mocha setup (mocha/chai with @types), add the test-setup.js used above, and mocha-opts. You can exclude the test-helper.js. VSCode doesn't see the global expect at all. I tried setting an alias for the global.export and while it removed the squiggles, it could not access the reference, and I had to add the chai import in the end. – DBrown Oct 15 '18 at 03:34
  • As far as I can tell, its not a VSCode issue, it is an issue with TS-Node not understanding global updates – pjo336 Mar 20 '19 at 15:42
  • The missing piece for me was including a `.d.ts` file with: `declare const expect: Chai.ExpectStatic` or, if you're using `sinon-chai`, `declare const expect: SinonChai.ExpectStatic`. The latter still shows a squiggly line for me in the .d.ts file, but it works correctly. An important point is that there are _not_ any `import` statements in the `.d.ts` file, otherwise it doesn't work correctly. – Daniel Schaffer May 22 '19 at 19:32
  • @DanielSchaffer How do you conditionally include a .d.ts only in tests? If not conditionally included, I assume those types would be ambient (global) in all of your code, even in non-tests where chai has not been imported. – Joe Bowbeer Dec 13 '19 at 20:51
  • @JoeBowbeer it depends on how your tsconfig file is set up. If you aren't explicitly specifying `include` or `files`, it should just get picked up automatically. Otherwise, you'll need to include the file path or a glob that will pick it up in the appropriate array. – Daniel Schaffer Dec 26 '19 at 16:46
  • I did a while back using the tsconfigs-path library: https://www.npmjs.com/package/tsconfig-paths You can see a setup i have used here: https://github.com/pjo336/tea/tree/master/tests – pjo336 Apr 08 '20 at 20:08

0 Answers0