-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjest.dang
More file actions
117 lines (103 loc) · 2.78 KB
/
Copy pathjest.dang
File metadata and controls
117 lines (103 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
Toolchain to execute Jest tests
"""
type Jest {
"""
The source directory for the project.
"""
pub source: Directory!
"""
The base image to use.
"""
pub baseImageAddress: String!
"""
The package manager to use.
"""
pub packageManager: String!
new(
ws: Workspace!,
baseImageAddress: String! = "node:25-alpine",
packageManager: String! = "npm",
) {
self.source = ws.directory("/", exclude: ["**/node_modules", "/dist", "/build"])
self.baseImageAddress = baseImageAddress
self.packageManager = packageManager
self
}
"""
Execute the tests
"""
pub test(
"""
List of files to test
"""
files: [String!]! = [],
"""
Run build before test
"""
build: Boolean! = false,
"""
Use jest-defined environment
"""
useEnv: Boolean! = false,
"""
Flags to pass to jest
"""
flags: [String!]! = [],
): Void @check {
let n = nodejs(source, baseImageAddress, packageManager)
let runtime = n.base
# optionally build first
if (build) {
runtime = runtime.withExec([packageManager, "run", "build"])
}
# install the local build of the otel library
runtime = n.withInstallLocalPackage(runtime, buildLibrary)
# use the automatic environment instead of a custom jest environment
if (!useEnv) {
let packageType = container
.from("alpine:3")
.withExec(["apk", "add", "jq"])
.withFile("/package.json", source.file("package.json"))
.withExec(["jq", ".type", "/package.json"])
.stdout
let opts = "$NODE_OPTIONS --import @dagger.io/jest/register "
if (packageType.contains("commonjs")) {
opts = "$NODE_OPTIONS --require @dagger.io/jest/register "
}
runtime = runtime
.withEnvVariable("NODE_OPTIONS", opts, expand: true)
}
# mount and install the otel library and then execute the tests
runtime
.withEnvVariable("DAGGER_JEST_CACHE_BUSTER", UUID.v7)
.withExec(["npx", "jest"] + flags + files)
.sync
null
}
"""
List the tests
"""
pub list: String {
let cmd = ["npx", "jest", "--listTests"]
nodejs(source, baseImageAddress, packageManager)
.base
.withExec(cmd)
.stdout
}
"""
Build the otel library
"""
let buildLibrary: Directory! {
let module = currentModule.source
container
.from("oven/bun:1.3.0-alpine@sha256:37e6b1cbe053939bccf6ae4507977ed957eaa6e7f275670b72ad6348e0d2c11f")
.withMountedCache("/root/.bun/install/cache", cacheVolume("library-node-modules"))
.withEnvVariable("BUN_INSTALL_CACHE_DIR", "/root/.bun/install/cache")
.withDirectory("/src", module)
.withWorkdir("/src")
.withExec(["bun", "install"])
.withExec(["bun", "run", "build"])
.directory("/src")
}
}