Skip to content

Commit

Permalink
fix: Add test script to validate import/require
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Jul 17, 2023
1 parent 4e38d39 commit 48e8a74
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ jobs:

- name: 🧪 run tests
run: npm run test:${{ matrix.lsp }}

- name: 🧪 run import/requires tests
run: npm run test:import
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"test:reentrancy": "hardhat test --no-compile tests/Reentrancy/Reentrancy.test.ts",
"test:reentrancyinit": "hardhat test --no-compile tests/Reentrancy/ReentrancyInit.test.ts",
"test:foundry": "forge test --no-match-test Skip -vvv --gas-report > gasreport.ansi",
"test:importRequire": "npm run build && npm run build:js && ./tests/importRequire.sh",
"clean": "hardhat clean && rm -rf module common",
"build": "hardhat dodoc && ts-node scripts/interfaceIds.ts && prettier -w ./docs ",
"build:js": "vite build && dts-bundle-generator --config dtsconfig.json",
Expand Down
118 changes: 118 additions & 0 deletions tests/importRequire.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env bash
mktmp=$(mktemp -d)
trap "rm -rf $mktmp" EXIT
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
ENDCOLOR="\033[0m"

if [ ! -f './dist/constants.cjs.js' ]
then
echo -e "${RED}Fail: No constants.cjs.js found${ENDCOLOR}"
exit 1
fi
if [ ! -f './dist/constants.es.js' ]
then
echo -e "${RED}Fail: No constants.es.js found${ENDCOLOR}"
exit 1
fi
if [ ! -f './package.json' ]
then
echo -e "${RED}Fail: No package.json found${ENDCOLOR}"
exit 1
fi

echo -e "${YELLOW}Packaging npm package${ENDCOLOR}"
npm pack

PACK="$(pwd)/$(ls | grep -E "^lukso.*tgz$")"
if [ ! -f "$PACK" ]
then
echo -e "${RED}Fail: No tgz pack file found${ENDCOLOR}"
exit 1
fi

echo -e "${YELLOW}Creating test directory${ENDCOLOR}"
cd $mktmp

echo -e "${YELLOW}Installing $PACK"
npm install "$PACK"

echo -e "${YELLOW}Creating cjs require test${ENDCOLOR}"
cat > test.cjs <<EOF
const pkg = require('@lukso/lsp-smart-contracts/package.json');
console.log("\x1b[32mSuccess: require package.json\x1b[0m");
const { INTERFACE_IDS } = require('@lukso/lsp-smart-contracts');
console.log("\x1b[32mSuccess: require { INTERFACE_IDS }}\x1b[0m");
EOF

echo -e "${YELLOW}Executing cjs require test${ENDCOLOR}"
if ! node test.cjs
then
echo "${RED}Fail: require failed${ENDCOLOR}"
exit 1
fi

echo -e "${YELLOW}Creating esm (ts) import test${ENDCOLOR}"
cat > test.ts <<EOF
import pkg from '@lukso/lsp-smart-contracts/package.json';
console.log("\x1b[32mSuccess: import package.json\x1b[0m");
import { INTERFACE_IDS } from '@lukso/lsp-smart-contracts';
console.log("\x1b[32mSuccess: import { INTERFACE_IDS }\x1b[0m");
EOF

echo -e "${YELLOW}Creating package.json type=module${ENDCOLOR}"
cat > package.json <<EOF
{
"type": "module"
}
EOF

echo -e "${YELLOW}Creating tsconfig.json to allow json import${ENDCOLOR}"
cat > tsconfig.json <<EOF
{
"compilerOptions": {
"target": "ES2018",
"module": "es2022",
"strict": true,
"outDir": "dist",
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"noImplicitAny": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"allowJs": true,
"baseUrl": ".",
"types": [
"webpack-env",
"jest",
"node",
"chrome"
],
"lib": [
"ES2018",
"dom",
"dom.iterable",
"scripthost"
],
"resolveJsonModule": true
},
"include": [
"./*.ts"
],
"exclude": [
"./node_modules"
]
}
EOF

echo -e "${YELLOW}Testing import${ENDCOLOR}"
if ! npx ts-node --esm test.ts
then
echo "${RED}Fail: import failed${ENDCOLOR}"
exit 1
fi

0 comments on commit 48e8a74

Please sign in to comment.