Skip to content

Commit

Permalink
test: add test case for #114 (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored Apr 5, 2023
1 parent 6b80b99 commit a486b66
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
e2e/
node_modules
example/node_modules
jest/tests/node_modules
1 change: 1 addition & 0 deletions e2e/update_snapshots_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ update_snapshot_e2e_test example/snapshots/__snapshots__/link.test.js.snap //exa
update_snapshot_e2e_test example/snapshots_files/__snapshots__/link.test.js.snap //example/snapshots_files:test
update_snapshot_e2e_test example/custom_snapshot_resolver/__my_snapshots__/link.test.js.snap //example/custom_snapshot_resolver:test
update_snapshot_e2e_test example/custom_snapshot_resolver_files/link.test.js.snap //example/custom_snapshot_resolver_files:test
update_snapshot_e2e_test jest/tests/package_json_module/__snapshots__/link.test.js.snap //jest/tests/package_json_module:test

echo "All tests passed"
2 changes: 1 addition & 1 deletion example/snapshots/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jest_test(
snapshots = True,
)

# The tests and tested are normally in their own targets.
# Tests are normally in their own targets.
js_library(
name = "tests",
testonly = True,
Expand Down
3 changes: 3 additions & 0 deletions jest/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//jest:defs.bzl", "jest_test")

npm_link_all_packages(name = "node_modules")

exports_files([
"case3.jest.config.js",
"case3.test.js",
Expand Down
15 changes: 15 additions & 0 deletions jest/tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"private": true,
"dependencies": {
"react": "18.2.0"
},
"devDependencies": {
"@babel/core": "7.20.2",
"@babel/preset-env": "7.20.2",
"@babel/preset-react": "7.18.6",
"@jest/test-sequencer": "29.3.1",
"babel-jest": "29.3.1",
"jest-junit": "15.0.0",
"react-test-renderer": "18.2.0"
}
}
50 changes: 50 additions & 0 deletions jest/tests/package_json_module/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//jest:defs.bzl", "jest_test")

jest_test(
name = "test",
config = "jest.config.js",
data = [
"babel.config.cjs",
"package.json",
":lib",
":tests",
"//jest/tests:node_modules/@babel/core",
"//jest/tests:node_modules/@babel/preset-env",
"//jest/tests:node_modules/@babel/preset-react",
"//jest/tests:node_modules/babel-jest",
],
snapshots = True,
)

# Tests are normally in their own targets.
js_library(
name = "tests",
testonly = True,
srcs = [
"greetings.test.js",
"link.test.js",
"simple.test.js",
],
deps = [
"//jest/tests:node_modules/react-test-renderer",
],
)

js_library(
name = "lib",
srcs = [
"greetings.js",
"link.js",
],
deps = [
"//jest/tests:node_modules/react",
],
)

# TEST: Ensure the {name}_update_snapshots target builds successfully
build_test(
name = "update_snapshots_build",
targets = [":test_update_snapshots"],
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders correctly 1`] = `
<h2>
Hello
World
!
</h2>
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders correctly 1`] = `
<a
href="https://aspect.build"
>
Aspect
</a>
`;
6 changes: 6 additions & 0 deletions jest/tests/package_json_module/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
"@babel/preset-env",
["@babel/preset-react", { runtime: "automatic" }],
],
};
3 changes: 3 additions & 0 deletions jest/tests/package_json_module/greetings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Greetings(props) {
return <h2>Hello {props.name}!</h2>;
}
7 changes: 7 additions & 0 deletions jest/tests/package_json_module/greetings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import renderer from "react-test-renderer";
import Greetings from "./greetings";

it("renders correctly", () => {
const tree = renderer.create(<Greetings name="World" />).toJSON();
expect(tree).toMatchSnapshot();
});
4 changes: 4 additions & 0 deletions jest/tests/package_json_module/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
testEnvironment: "node",
testMatch: ["**/*.test.js"],
};
3 changes: 3 additions & 0 deletions jest/tests/package_json_module/link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Link({ page, children }) {
return <a href={page || "#"}>{children}</a>;
}
9 changes: 9 additions & 0 deletions jest/tests/package_json_module/link.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import renderer from "react-test-renderer";
import Link from "./link";

it("renders correctly", () => {
const tree = renderer
.create(<Link page="https://aspect.build">Aspect</Link>)
.toJSON();
expect(tree).toMatchSnapshot();
});
3 changes: 3 additions & 0 deletions jest/tests/package_json_module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
3 changes: 3 additions & 0 deletions jest/tests/package_json_module/simple.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test("2+2==4", () => {
expect(2 + 2).toEqual(4);
});
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
packages:
- "example"
- "jest/tests"

0 comments on commit a486b66

Please sign in to comment.