Skip to content

Commit

Permalink
test typescript interop
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored and anonrig committed Jan 4, 2023
1 parent 93c9da4 commit 4b96311
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 7 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/typescript-interop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Typescript Interop

on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read

jobs:
ts-interop-test:
runs-on: ubuntu-latest

strategy:
matrix:
package-json-type:
- commonjs
- module
tsconfig-json-module:
- CommonJS
- ESNext
- Node16
- Nodenext
tsconfig-json-module-resolution:
- Node
- Node16
- Nodenext
exclude:
- package-json-type: commonjs
tsconfig-json-module: ESNext
- package-json-type: module
tsconfig-json-module: CommonJS
- package-json-type: module
tsconfig-json-module: Node16
tsconfig-json-module-resolution: Node
- package-json-type: module
tsconfig-json-module: NodeNext
tsconfig-json-module-resolution: Node

fail-fast: false

steps:
- uses: actions/checkout@v3
with:
persist-credentials: false
- uses: actions/setup-node@v3
with:
node-version: 18
- run: |
npm install --ignore-scripts &&
npm link &&
node scripts/create-ts-interop-test &&
cd test_interop &&
npm i --ignore-scripts &&
npm link fast-querystring &&
npm run test-interop
env:
PACKAGE_JSON_TYPE: ${{ matrix.package-json-type }}
TSCONFIG_MODULE: ${{ matrix.tsconfig-json-module }}
TSCONFIG_MODULE_RESOLUTION: ${{ matrix.tsconfig-json-module-resolution }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
package-lock.json
test_interop
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ benchmark
.idea
.github
rome.json
scripts
test_interop
14 changes: 10 additions & 4 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
declare namespace FastQuerystring {
type FastQueryString = {
stringify(value: Record<string, any>): string;
parse(value: string): Record<string, any>;
};

declare namespace fastQueryString {
export function stringify(value: Record<string, any>): string;
export function parse(value: string): Record<string, any>;

const fqs: FastQueryString;
export { fqs as default };
}

export function stringify(value: Record<string, any>): string;
export function parse(value: string): Record<string, any>;
export default FastQuerystring;
export = fastQueryString;
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ const fastQuerystring = {
*/
module.exports = fastQuerystring;
module.exports.default = fastQuerystring;
module.exports.parse = parse;
module.exports.stringify = stringify;
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"name": "fast-querystring",
"version": "1.0.0",
"description": "A fast alternative to legacy querystring module",
"main": "lib/index.js",
"type": "commonjs",
"types": "lib/index.d.ts",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"scripts": {
"format": "rome format . --write",
"format:ci": "rome ci .",
Expand Down
71 changes: 71 additions & 0 deletions scripts/create-ts-interop-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node
const path = require("path");
const fs = require("fs").promises;

const packageJsonType =
process.argv[2] || process.env.PACKAGE_JSON_TYPE || "commonjs";
const tsconfigModule = process.argv[2] || process.env.TSCONFIG_MODULE || "Node";
const tsconfigModuleResolution =
process.argv[2] || process.env.TSCONFIG_MODULE_RESOLUTION || "Node";

const rootPath = path.join(__dirname, "..");
const testPath = path.join(rootPath, "test_interop");

const indexTs = `import fastQuerystring from 'fast-querystring'
import { stringify, parse } from 'fast-querystring'
import * as fQuerystring from 'fast-querystring'
import { equal } from "assert"
equal(typeof fastQuerystring, 'object')
equal(typeof fastQuerystring.stringify, 'function')
equal(typeof fastQuerystring.parse, 'function')
equal(typeof fQuerystring.stringify, 'function')
equal(typeof fQuerystring.parse, 'function')
equal(typeof stringify, 'function')
equal(typeof parse, 'function')
`;

const tsconfigJson = JSON.stringify(
{
compilerOptions: {
module: tsconfigModule,
moduleResolution: tsconfigModuleResolution,
},
},
null,
2,
);

const packageJson = JSON.stringify(
{
name: "fqs-test",
version: "1.0.0",
description: "",
main: "index.js",
type: packageJsonType,
scripts: {
"test-interop": "tsc -p . && node index.js",
},
keywords: [],
author: "",
license: "ISC",
dependencies: {
"@types/node": "^18.11.10",
typescript: "^4.9.3",
},
},
null,
2,
);

async function main() {
await fs.mkdir(testPath);
await fs.writeFile(path.join(testPath, "package.json"), packageJson);
await fs.writeFile(path.join(testPath, "tsconfig.json"), tsconfigJson);
await fs.writeFile(path.join(testPath, "index.ts"), indexTs);
}

main(process.argv).catch((err) => {
console.error(err);
process.exit(1);
});

0 comments on commit 4b96311

Please sign in to comment.