1

I have some code in my TypeScript tests, say:

function foo() {
    return 'bar';
}

which I'd like to share with all my tests. The simple solution is to copy the function into each *_test.ts file.

How can I "import" this code from foo_test_helper.ts in a truffle test?

If possible, I'd like to avoid adding Babel to my build. It should work with a project created by npx truffle init. If you suggest Babel, then please include (pointers to) the instructions necessary to set this up.

0

Recipe:

  1. Create a file foo.ts in the test/ folder (next to the other tests).
  2. At the bottom of the file, add:

    module.exports = {
        foo: foo // export function foo() defined above as "foo"
    };
    
  3. Import the function in your other tests:

    const { foo } = require('./foo.ts');
    

If you need to export more functions, add the names to the exports object and list them in the require() assignment, separated by commas. Order is not important, the variables will be matched by name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.