How do you run Typescript tests with Mocha for a NPM package that's mostly Javascript?
I have some Javascript/Node.js code that I'm trying to write a unittest for. Since the code does processing on audio samples recorded through the browser, I need to test it by loading wav files from the local dev filesystem.
Since that's a little easier to do in Typescript, I'd like to write my unittests in Typescript and Mocha.
However, Mocha can't seem to find even a basic stub test.
My package structure looks like:
src
index.js
test
test.ts
mocha.opts
Where test.ts looks like:
import MyPackage from '../src';
describe('Simple Math Test', () => {
it('should return 2', () => {
assert.equal(1 + 1, 2);
});
});
And my mocha.opts looks like:
--require ts-node/register
--recursive
--reporter spec
--ui bdd
--timeout 20000
test/**/*.ts
But when I run mocha
from my project root, it outputs:
Error: No test files found
If I change my test.ts
to test.js
, then it finds it. How do I fix this?
Edit: I found if I run the command mocha -r ts-node/register test/test.ts
, then it seems to see the Typescript test.
However, it still can't import my Javascript package:
Cannot find module '../src' or its corresponding type declarations.
import MyPackage from '../src';