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

test: add a testing example using jest #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ jobs:
restore-keys: |
${{ runner.os }}-yarn-

- name: Mount bazel cache
uses: actions/cache@v1
with:
path: "/home/runner/.cache/bazel"
key: bazel
# - name: Mount bazel cache
# uses: actions/cache@v1
# with:
# path: "/home/runner/.cache/bazel"
# key: bazel

- run: yarn

- run: yarn run build

# - run: yarn run test
- run: yarn run test

- run: yarn run shutdown
3 changes: 2 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
exports_files(
[
"tsconfig.json"
"tsconfig.json",
"jest.config.js",
],
visibility = ["//visibility:public"],
)
Expand Down
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/'],
reporters: ['default']
}
40 changes: 40 additions & 0 deletions libs/javascript/jest_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("//rules:jest.bzl", "jest_test")

filegroup(
name = 'src',
srcs = [
'index.js',
],
)

filegroup(
name = 'test_src',
srcs = [':src'] + glob(['__tests__/**/*.test.js']),
)

pkg_npm(
name = 'pkg',
srcs = [
'package.json',
],
deps = [
':src'
],
visibility = ['//visibility:public'],
)

jest_test(
name = 'test',
srcs = [":test_src"],
jest_config = "//:jest.config.js",
tags = [
# Need to set the pwd to avoid jest needing a runfiles helper
# Windows users with permissions can use --enable_runfiles
# to make this test work
"no-bazelci-windows",
],
deps = [
"@npm//:node_modules"
],
)
7 changes: 7 additions & 0 deletions libs/javascript/jest_test/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const index = require('../index.js')

describe('example jest test', () => {
test('can execute code', () => {
expect(index()).toEqual('Hello world')
})
})
1 change: 1 addition & 0 deletions libs/javascript/jest_test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'Hello world'
8 changes: 8 additions & 0 deletions libs/javascript/jest_test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "lib-javascript-jest-test",
"version": "1.0.0",
"description": "Javascript package with a jest test",
"main": "index.js",
"author": "",
"license": "MIT"
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
"@bazel/rollup": "1.5.0",
"@bazel/typescript": "1.5.0",
"@types/node": "^12.11.1",
"jest": "^25.2.4",
"rollup": "^1.25.2",
"typescript": "^3.8.0"
},
"scripts": {
"build": "bazelisk build //...",
"test": "bazelisk test //...",
"clean": "bazelisk clean",
"shutdown": "bazelisk shutdown"
"shutdown": "bazelisk shutdown",
"sync": "bazelisk sync"
}
}
Empty file added rules/BUILD.bazel
Empty file.
20 changes: 20 additions & 0 deletions rules/jest.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@npm//jest-cli:index.bzl", _jest_test = "jest_test")

def jest_test(name, srcs, deps, jest_config, **kwargs):
"""A macro around the autogenerated jest_test rule"""
args = [
"--no-cache",
"--no-watchman",
"--ci",
]
args.extend(["--config", "$(location %s)" % jest_config])

for src in srcs:
args.extend(["--runTestsByPath", "$(locations %s)" % src])

_jest_test(
name = name,
data = [jest_config] + srcs + deps,
args = args,
**kwargs
)
Loading