coderaiser / supertape

📼 Simplest high speed testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changes to `--require`

tommy-mitchell opened this issue · comments

commented

In #3, you mentioned that the --require flag could be used for JIT transpilation:

How does the --require flag work? Tape says that it can be used for JIT compilation - does this mean that the supertape CLI could support TypeScript through a required module?

Yes [this could work]

A quick Google search brought me to this article about using tape with TypeScript by requiring ts-node/register/transpile-only (effectively, a module that transpiles TS without type checking). However, trying

$ supertape -r ts-node/register/transpile-only test.ts

Fails with an error message about the .ts extension:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for test.ts

Looking into tape's implementation for --require, it has an import-or-require.js script:

function importOrRequire(file) {
  const ext = extnamePath(file);

  if (ext === '.mjs' || (ext === '.js' && getPackageType.sync(file) === 'module'))
    return import(pathToFileURL(file).href);

  require(file);
};

Compared to Supertape's simple-import.js, which simply uses a dynamic import:

module.exports.simpleImport = (a) => import(a);

I've tested out a simple copy and paste implementation and can successfully run a TypeScript test:

// lib/cli.js

// in function 'cli'
for (const file of files) {
  const resolved = resolvePath(cwd, file);
  promises.push(importOrRequire(resolved));
}
// test.ts
import test from "supertape";

test("test: from ts-node", async t => {
  t.pass("using 'supertape' binary");
  t.end();
});
$ supertape -r ts-node/register/transpile-only test.ts
TAP version 13

1..1
# tests 1
# pass 1

# ✅ ok

What do you think about implementing something like this?

PR’s are welcome :)