Skip to content

Commit a2c58ea

Browse files
author
Maia McCormick
authored
example of test extension for js (#12)
1 parent 6d82977 commit a2c58ea

File tree

17 files changed

+4692
-1
lines changed

17 files changed

+4692
-1
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ jobs:
99
- setup_remote_docker:
1010
version: 19.03.12
1111
- run: apt install -y python
12+
- run: apt install -y npm
13+
- run: npm install -g yarn
1214
- run: ctlptl create cluster kind --registry=ctlptl-registry && test/test.sh

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
**/node_modules
22
**/start-time.txt
3-
*/.idea
3+
*/.idea
4+
**/yarn-error.log
5+
6+
**/tilt_modules

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This progression of examples shows how to start, and incrementally update your p
1717

1818
- [101-debugger](101-debugger): The recommended setup, exposing a debugger on port 9229 that you
1919
can connect to with your favorite NodeJS debugger (chrome://inspect, VSCode, IntelliJ, etc)
20+
- [tests-example](tests-example): an example of how to use Tilt to run your tests for you as you iterate
2021

2122
## License
2223

test/test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ tilt down --file 3-recommended/Tiltfile
2424
echo "Testing 101-debugger"
2525
tilt ci --file 101-debugger/Tiltfile
2626
tilt down --file 101-debugger/Tiltfile
27+
28+
echo "Testing tests-example"
29+
tilt ci --file tests-example/Tiltfile
30+
tilt down --file tests-example/Tiltfile

tests-example/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
yarn-error.log

tests-example/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM node:10
2+
3+
# Default value; will be overridden by build_args, if passed
4+
ARG node_env=production
5+
6+
ENV NODE_ENV $node_env
7+
8+
WORKDIR /app
9+
10+
ADD package.json .
11+
ADD yarn.lock .
12+
RUN yarn install
13+
14+
ADD . .
15+
16+
ENTRYPOINT [ "node", "/app/index.js" ]

tests-example/Tiltfile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- mode: Python -*
2+
3+
# Tilt can run your tests for you, so you get fast feedback while iterating on your app
4+
include('Tiltfile.tests')
5+
6+
k8s_yaml('kubernetes.yaml')
7+
k8s_resource('example-nodejs', port_forwards=8000,
8+
resource_deps=['deploy']
9+
)
10+
11+
# Records the current time, then kicks off a server update.
12+
# Normally, you would let Tilt do deploys automatically, but this
13+
# shows you how to set up a custom workflow that measures it.
14+
local_resource(
15+
'deploy',
16+
'python now.py > start-time.txt',
17+
)
18+
19+
# Add a live_update rule to our docker_build
20+
congrats = "🎉 Congrats, you ran a live_update! 🎉"
21+
docker_build('example-nodejs-image', '.',
22+
build_args={'node_env': 'development'},
23+
entrypoint='yarn run nodemon /app/index.js',
24+
live_update=[
25+
sync('.', '/app'),
26+
run('cd /app && yarn install', trigger=['./package.json', './yarn.lock']),
27+
28+
# if all that changed was start-time.txt, make sure the server
29+
# reloads so that it will reflect the new startup time
30+
run('touch /app/index.js', trigger='./start-time.txt'),
31+
32+
# add a congrats message!
33+
run('sed -i "s/Hello cats!/{}/g" /app/views/index.mustache'.
34+
format(congrats)),
35+
])
36+

tests-example/Tiltfile.tests

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- mode: Python -*-
2+
3+
# For more on the `test_jest_yarn` extension:
4+
# https://github.com/tilt-dev/tilt-extensions/tree/master/tests/javascript
5+
# For more on tests in Tilt: https://docs.tilt.dev/tests_in_tilt.html
6+
load('ext://tests/javascript', 'test_jest_yarn')
7+
8+
# Jest is smart about what tests it runs (by default, only runs tests related to files
9+
# that have changed since your last commit), so it won't be too expensive to run this
10+
# on every file change
11+
# Make sure you have "scripts.test" configured in "package.json"
12+
# (this is done automatically for you in some frameworks, e.g. React)
13+
test_jest_yarn('unit-tests', '.',
14+
with_install=True, # make sure all dependencies are present locally so you can run tests
15+
16+
# in CI mode, run ALL tests (not just those related to files changed since last commit)
17+
only_changed=config.tilt_subcommand != 'ci')

tests-example/foo.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
test("foo is okay", () => {
2+
expect("foo").toEqual("foo")
3+
});
4+
5+
test("bar is okay", () => {
6+
expect("bar").toEqual("bar")
7+
});
8+
9+
test("baz is okay", () => {
10+
expect("baz").toEqual("baz")
11+
});

tests-example/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Gratefully adapted from https://github.com/HemingwayLee/sample-mustache-express
3+
*/
4+
const fs = require('fs');
5+
6+
const express = require('express');
7+
const app = express();
8+
const mustacheExpress = require('mustache-express');
9+
10+
let timeSince = 'N/A';
11+
12+
app.engine('mustache', mustacheExpress());
13+
14+
app.set('view engine', 'mustache');
15+
app.set('views', __dirname + '/views');
16+
app.use(express.static('public'));
17+
18+
app.get('/', (req, res) => {
19+
res.render('index.mustache', {
20+
time: timeSince,
21+
});
22+
});
23+
24+
app.listen(8000, () => {
25+
timeSince = getSecsSinceDeploy();
26+
console.log('Server running at http://localhost:8000/');
27+
});
28+
29+
function getSecsSinceDeploy() {
30+
let curTimeMs = new Date().getTime();
31+
let contents = fs.readFileSync('/app/start-time.txt', 'utf8');
32+
let startTimeMs = parseInt(contents.trim()) / 10**6;
33+
return ((curTimeMs - startTimeMs) / 10**3).toFixed(2)
34+
}

tests-example/index.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test("everything is okay", () => {
2+
expect(1).toEqual(1)
3+
});

tests-example/kubernetes.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: example-nodejs
5+
labels:
6+
app: example-nodejs
7+
spec:
8+
selector:
9+
matchLabels:
10+
app: example-nodejs
11+
template:
12+
metadata:
13+
labels:
14+
app: example-nodejs
15+
spec:
16+
containers:
17+
- name: example-nodejs
18+
image: example-nodejs-image
19+
ports:
20+
- containerPort: 8000

tests-example/now.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import time
2+
3+
print("%d" % (float(time.time()) * 1000 * 1000 * 1000))

tests-example/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "tilt-example-nodejs",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"express": "^4.17.1",
8+
"immutable": "^3.8.2",
9+
"mustache-express": "^1.3.0"
10+
},
11+
"devDependencies": {
12+
"jest": "^26.6.3",
13+
"nodemon": "^2.0.2"
14+
},
15+
"scripts": {
16+
"test": "jest"
17+
}
18+
}

tests-example/public/pets.png

182 KB
Loading

tests-example/views/index.mustache

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<body style="font-size: 30px; font-family: sans-serif; margin: 0;">
4+
<div style="display: flex; flex-direction: column; width: 100vw; height: 100vh; align-items: center; justify-content: center;">
5+
<img src="pets.png" style="max-width: 30vw; max-height: 30vh;">
6+
<div>Hello cats!</div>
7+
<div>Time from "deploy" button pressed → server up: {{time}}s</div>
8+
</div>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)