In my project, I am looking to create a test helper file called test-helper.ts
. This file will be responsible for setting up a global set of dependencies for my test files and will also include some utility functions. Here is an example of what it may look like:
import 'mocha';
import * as chai from 'chai';
import * as Promise from "bluebird";
import './testing/test-console'; // TypeScript declaration
import '../src/shared/base-defs';
import { stdout, stderr } from 'test-console';
const expect = chai.expect;
function prepareEnv(env) { ... }
In the actual test file, test-spec.ts
, I would like to import this helper file using something similar to this:
import 'test-helper';
declare('my tests', () => { ... }
I want to ensure that the globally loaded dependencies such as mocha, chai, and others are accessible within the test file. Additionally, I would like to have my helper functions like prepareEnv()
available either in the global namespace or in a separate helper namespace. Perhaps I can achieve this by creating a namespace like so:
declare namespace Helpers { ... }
and placing the function inside it?
Update
Upon further exploration, it seems that functions cannot be implemented in ambient contexts, so my initial idea of declaring a namespace should be disregarded. Instead, I am now importing a file called test-functions.ts
into the test-helper.ts
file:
import * as helpers from './testing/test-functions.ts';
While this is a step in the right direction, the main challenge still remains - ensuring that the ambient/global declarations in the test-helper.ts
file are accessible to test files like test-spec.ts
.