Skip to content

Commit

Permalink
chore: add tests covering JS files
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed May 16, 2023
1 parent e4c713a commit fd6ec53
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ require('./myFile.ts'); // works

### Limitations

- You can't load `.ts` files from static `import` statements from JS files. You
have to use dynamic `import()`, or convert the file to TS.
- You can't import `.ts` files from ESM. If you want to use ESM syntax, you have
convert the file to TS and use `.ts` file extension.
- By default, we only support `.js` and `.ts` file extensions.
- Windows would only support the `node -r @transloadit/ts-fly …` form, as the
executable is a POSIX shell script.
Expand Down
78 changes: 78 additions & 0 deletions test/jsEntryPoint.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Buffer } from 'node:buffer';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';
import { once } from 'node:events';
import { spawn } from 'node:child_process';
import { platform, tmpdir } from 'node:os';
import assert from 'node:assert';
import fs from 'node:fs/promises';

const ROOT_DIR = new URL('../', import.meta.url);
const { bin } = JSON.parse(
await fs.readFile(new URL('./package.json', ROOT_DIR), 'utf-8'),
);

const tmpDir = await fs.mkdtemp(join(tmpdir(), 'tsen-test-'));

await Promise.all([
fs.writeFile(
join(tmpDir, 'entryPoint.js'),
`#!${
// macOS doesn't support shebangs pointing at scripts for some reason
(platform() === 'darwin' ? '/usr/bin/env ' : '') +
fileURLToPath(new URL(bin, ROOT_DIR))
}\n
'use strict';
console.log('require', require('./require.ts'));
import('./dynamic.ts').then(module => console.log('dynamic TS', Object.keys(module)), console.error);
import('./dynamic.mjs').then(module => console.log('dynamic ESM', Object.keys(module)), console.error);
\n`,
'ascii',
),

fs.writeFile(join(tmpDir, 'require.ts'), 'export const a = 1'),
fs.writeFile(join(tmpDir, 'reexported.js'), 'exports.c = 1'),

fs.writeFile(
join(tmpDir, 'dynamic.ts'),
`"use strict";
export default 1;
export const a = 1;
export const b = 1;
export * from "./reexported.js";
\n`,
),

fs.writeFile(
join(tmpDir, 'dynamic.mjs'),
`
export default 1;
export const a = 1;
export const b = 1;
export * from "./reexported.js";
\n`,
),
]);

await Promise.all([fs.chmod(join(tmpDir, 'entryPoint.js'), 0o777)]);

try {
const cp = spawn(join(tmpDir, 'entryPoint.js'));

const [exitStatus, stdoutArray, stderrArray] = await Promise.all([
once(cp, 'exit'),
cp.stdout.toArray(),
cp.stderr.toArray(),
]);

const stdout = Buffer.concat(stdoutArray).toString('utf-8');
const stderr = Buffer.concat(stderrArray).toString('utf-8');

assert.strictEqual(stderr, '');
assert.match(stdout, /^require \{ a: 1 \}$/m);
assert.match(stdout, /^dynamic TS \[ 'default', 'a', 'b', 'c' \]$/m);
assert.match(stdout, /^dynamic ESM \[ 'a', 'b', 'c', 'default' \]$/m);
assert.deepStrictEqual(exitStatus, [0, null]);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}

0 comments on commit fd6ec53

Please sign in to comment.