Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to ensure cross-workspace reloading works ok #175

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules
pkg
.envrc.local
test/supervise/run-scratch.ts
test/reload/run-scratch.ts
**/run-scratch.ts
**/run-scratch.ts
.direnv
6 changes: 6 additions & 0 deletions integration-test/reload-cross-workspace-lazy/main/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"module": {
"type": "commonjs",
"lazy": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "main",
"version": "1.0.0",
"main": "index.js",
"type": "commonjs",
"dependencies": {
"side": "workspace:*"
}
}
11 changes: 11 additions & 0 deletions integration-test/reload-cross-workspace-lazy/main/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import http from "http";
import { message } from "side/run-scratch";

const requestListener = function (req, res) {
res.writeHead(200);
res.end(message);
};

const server = http.createServer(requestListener);
server.listen(8080);
console.warn("Listening on 8080");
15 changes: 15 additions & 0 deletions integration-test/reload-cross-workspace-lazy/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- "main"
- "side"
6 changes: 6 additions & 0 deletions integration-test/reload-cross-workspace-lazy/side/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"module": {
"type": "commonjs",
"lazy": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "side",
"version": "1.0.0",
"main": "index.js",
"type": "commonjs"
}
1 change: 1 addition & 0 deletions integration-test/reload-cross-workspace-lazy/side/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "Hello, World!";
50 changes: 50 additions & 0 deletions integration-test/reload-cross-workspace-lazy/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -e


# kill the server when this script exits
trap "kill -9 0" INT TERM
trap 'kill $(jobs -p)' EXIT

# setup the pnpm workspace with multiple packages
cd $DIR
pnpm install

# make a copy of the run.ts file in the side package for us to modify
cp $DIR/side/run.ts $DIR/side/run-scratch.ts

# run a server in the main package in the background
$DIR/../../pkg/wds.bin.js $@ --watch --commands $DIR/main/run.ts &

max_retry=5
counter=0

set +e
until curl -s localhost:8080 | grep "World"
do
sleep 1
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
echo "Trying again. Try #$counter"
((counter++))
done

echo "Made initial request to server"

# modify the file in the side package and expect the main script to reload
sed -i 's/Hello, World/Hey, Pluto/g' $DIR/side/run-scratch.ts

echo "Made change to side package"

counter=0
until curl -s localhost:8080 | grep "Pluto"
do
sleep 1
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
echo "Trying again. Try #$counter"
((counter++))
done

echo "Made new request to reloaded server"

exit 0
9 changes: 9 additions & 0 deletions integration-test/reload-cross-workspace/main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "main",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"dependencies": {
"side": "workspace:*"
}
}
11 changes: 11 additions & 0 deletions integration-test/reload-cross-workspace/main/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import http from "http";
import { message } from "side/run-scratch";

const requestListener = function (req, res) {
res.writeHead(200);
res.end(message);
};

const server = http.createServer(requestListener);
server.listen(8080);
console.warn("Listening on 8080");
15 changes: 15 additions & 0 deletions integration-test/reload-cross-workspace/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions integration-test/reload-cross-workspace/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- "main"
- "side"
6 changes: 6 additions & 0 deletions integration-test/reload-cross-workspace/side/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "side",
"version": "1.0.0",
"main": "index.js",
"type": "module"
}
1 change: 1 addition & 0 deletions integration-test/reload-cross-workspace/side/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const message = "Hello, World!";
50 changes: 50 additions & 0 deletions integration-test/reload-cross-workspace/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
set -e


# kill the server when this script exits
trap "kill -9 0" INT TERM
trap 'kill $(jobs -p)' EXIT

# setup the pnpm workspace with multiple packages
cd $DIR
pnpm install

# make a copy of the run.ts file in the side package for us to modify
cp $DIR/side/run.ts $DIR/side/run-scratch.ts

# run a server in the main package in the background
$DIR/../../pkg/wds.bin.js $@ --watch --commands $DIR/main/run.ts &

max_retry=5
counter=0

set +e
until curl -s localhost:8080 | grep "World"
do
sleep 1
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
echo "Trying again. Try #$counter"
((counter++))
done

echo "Made initial request to server"

# modify the file in the side package and expect the main script to reload
sed -i 's/Hello, World/Hey, Pluto/g' $DIR/side/run-scratch.ts

echo "Made change to side package"

counter=0
until curl -s localhost:8080 | grep "Pluto"
do
sleep 1
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
echo "Trying again. Try #$counter"
((counter++))
done

echo "Made new request to reloaded server"

exit 0
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@
"node": ">=16.0.0"
},
"dependencies": {
"@pnpm/find-workspace-dir": "^1000.0.0",
"@swc/core": "^1.9.3",
"@swc/helpers": "^0.5.13",
"find-root": "^1.1.0",
"find-yarn-workspace-root": "^2.0.0",
"fs-extra": "^11.2.0",
"globby": "^11.1.0",
"lodash": "^4.17.20",
"oxc-resolver": "^1.12.0",
"micromatch": "^4.0.8",
"node-object-hash": "^3.0.0",
"oxc-resolver": "^1.12.0",
"pkg-dir": "^5.0.0",
"watcher": "^2.3.1",
"write-file-atomic": "^6.0.0",
Expand All @@ -61,6 +63,7 @@
"@types/find-root": "^1.1.4",
"@types/fs-extra": "^11.0.4",
"@types/lodash": "^4.17.13",
"@types/micromatch": "^4.0.9",
"@types/node": "^22.9.3",
"@types/write-file-atomic": "^4.0.3",
"@types/yargs": "^15.0.19",
Expand Down
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions spec/SwcCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ test("throws if the file is ignored", async () => {
}
}

expect(error).toBeDefined();
expect(error).toBeTruthy();
expect(error?.ignoredFile).toBeTruthy();
expect(error?.message).toMatch(
/File .+ignored\.ts is imported but not being built because it is explicitly ignored in the wds project config\. It is being ignored by the provided glob pattern '!ignored\.ts', remove this pattern from the project config or don't import this file to fix./
/File .+ignored\.ts is imported but not being built because it is explicitly ignored in the wds project config\. It is being ignored by the provided glob pattern 'ignored\.ts', remove this pattern from the project config or don't import this file to fix./
);
});

Expand Down
17 changes: 0 additions & 17 deletions src/Options.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/Project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import type { Compiler } from "./Compiler.js";
import type { ProjectConfig } from "./Options.js";
import { PathTrie } from "./PathTrie.js";
import type { ProjectConfig } from "./ProjectConfig.js";
import type { Supervisor } from "./Supervisor.js";
import { log } from "./utils.js";

Expand Down
Loading
Loading