|
| 1 | +// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license. |
| 2 | + |
| 3 | +import * as assert from "@std/assert"; |
| 4 | +import { |
| 5 | + createEsbuildBuilderState, |
| 6 | + EsbuildBuilder, |
| 7 | + type EsbuildBuilderOptions, |
| 8 | +} from "./esbuild.ts"; |
| 9 | + |
| 10 | +Deno.test("createEsbuildBuilderState() creates valid state", () => { |
| 11 | + const options: EsbuildBuilderOptions = { |
| 12 | + buildID: "test-build-123", |
| 13 | + entrypoints: { |
| 14 | + "main": "./src/main.ts", |
| 15 | + "worker": "./src/worker.ts", |
| 16 | + }, |
| 17 | + dev: true, |
| 18 | + configPath: "./deno.json", |
| 19 | + target: "es2022", |
| 20 | + absoluteWorkingDir: "/project/root", |
| 21 | + jsx: "precompile", |
| 22 | + jsxImportSource: "@eser/jsx-runtime", |
| 23 | + basePath: "/static", |
| 24 | + }; |
| 25 | + |
| 26 | + const state = createEsbuildBuilderState(options); |
| 27 | + |
| 28 | + assert.assertEquals(state.options, options); |
| 29 | + assert.assertEquals(state.options.buildID, "test-build-123"); |
| 30 | + assert.assertEquals(state.options.dev, true); |
| 31 | + assert.assertEquals(state.options.target, "es2022"); |
| 32 | +}); |
| 33 | + |
| 34 | +Deno.test("EsbuildBuilder constructor initializes with state", () => { |
| 35 | + const options: EsbuildBuilderOptions = { |
| 36 | + buildID: "test-build", |
| 37 | + entrypoints: { "main": "./main.ts" }, |
| 38 | + dev: false, |
| 39 | + configPath: "./deno.json", |
| 40 | + target: "es2022", |
| 41 | + absoluteWorkingDir: "/test", |
| 42 | + }; |
| 43 | + |
| 44 | + const state = createEsbuildBuilderState(options); |
| 45 | + const builder = new EsbuildBuilder(state); |
| 46 | + |
| 47 | + assert.assertEquals(builder.state, state); |
| 48 | + assert.assertEquals(builder.state.options.buildID, "test-build"); |
| 49 | +}); |
| 50 | + |
| 51 | +Deno.test("EsbuildBuilderOptions supports string target", () => { |
| 52 | + const options: EsbuildBuilderOptions = { |
| 53 | + buildID: "test", |
| 54 | + entrypoints: {}, |
| 55 | + dev: false, |
| 56 | + configPath: "./deno.json", |
| 57 | + target: "es2022", |
| 58 | + absoluteWorkingDir: "/test", |
| 59 | + }; |
| 60 | + |
| 61 | + assert.assertEquals(options.target, "es2022"); |
| 62 | +}); |
| 63 | + |
| 64 | +Deno.test("EsbuildBuilderOptions supports array target", () => { |
| 65 | + const options: EsbuildBuilderOptions = { |
| 66 | + buildID: "test", |
| 67 | + entrypoints: {}, |
| 68 | + dev: false, |
| 69 | + configPath: "./deno.json", |
| 70 | + target: ["es2022", "chrome90", "firefox88"], |
| 71 | + absoluteWorkingDir: "/test", |
| 72 | + }; |
| 73 | + |
| 74 | + assert.assertInstanceOf(options.target, Array); |
| 75 | + assert.assertEquals(options.target.length, 3); |
| 76 | + assert.assertEquals(options.target[0], "es2022"); |
| 77 | +}); |
| 78 | + |
| 79 | +Deno.test("EsbuildBuilderOptions handles empty entrypoints", () => { |
| 80 | + const options: EsbuildBuilderOptions = { |
| 81 | + buildID: "test", |
| 82 | + entrypoints: {}, |
| 83 | + dev: false, |
| 84 | + configPath: "./deno.json", |
| 85 | + target: "es2022", |
| 86 | + absoluteWorkingDir: "/test", |
| 87 | + }; |
| 88 | + |
| 89 | + const state = createEsbuildBuilderState(options); |
| 90 | + assert.assertEquals(Object.keys(state.options.entrypoints).length, 0); |
| 91 | +}); |
| 92 | + |
| 93 | +Deno.test("EsbuildBuilderOptions handles multiple entrypoints", () => { |
| 94 | + const entrypoints = { |
| 95 | + "main": "./src/main.ts", |
| 96 | + "admin": "./src/admin.ts", |
| 97 | + "worker": "./src/worker.ts", |
| 98 | + }; |
| 99 | + |
| 100 | + const options: EsbuildBuilderOptions = { |
| 101 | + buildID: "test", |
| 102 | + entrypoints, |
| 103 | + dev: false, |
| 104 | + configPath: "./deno.json", |
| 105 | + target: "es2022", |
| 106 | + absoluteWorkingDir: "/test", |
| 107 | + }; |
| 108 | + |
| 109 | + const state = createEsbuildBuilderState(options); |
| 110 | + assert.assertEquals(Object.keys(state.options.entrypoints).length, 3); |
| 111 | + assert.assertEquals(state.options.entrypoints["main"], "./src/main.ts"); |
| 112 | + assert.assertEquals(state.options.entrypoints["admin"], "./src/admin.ts"); |
| 113 | + assert.assertEquals(state.options.entrypoints["worker"], "./src/worker.ts"); |
| 114 | +}); |
| 115 | + |
| 116 | +Deno.test("EsbuildBuilderOptions jsx configuration", () => { |
| 117 | + const options: EsbuildBuilderOptions = { |
| 118 | + buildID: "test", |
| 119 | + entrypoints: {}, |
| 120 | + dev: false, |
| 121 | + configPath: "./deno.json", |
| 122 | + target: "es2022", |
| 123 | + absoluteWorkingDir: "/test", |
| 124 | + jsx: "react-jsx", |
| 125 | + jsxImportSource: "react", |
| 126 | + }; |
| 127 | + |
| 128 | + assert.assertEquals(options.jsx, "react-jsx"); |
| 129 | + assert.assertEquals(options.jsxImportSource, "react"); |
| 130 | +}); |
| 131 | + |
| 132 | +Deno.test("EsbuildBuilderOptions optional jsx fields", () => { |
| 133 | + const options: EsbuildBuilderOptions = { |
| 134 | + buildID: "test", |
| 135 | + entrypoints: {}, |
| 136 | + dev: false, |
| 137 | + configPath: "./deno.json", |
| 138 | + target: "es2022", |
| 139 | + absoluteWorkingDir: "/test", |
| 140 | + }; |
| 141 | + |
| 142 | + assert.assertEquals(options.jsx, undefined); |
| 143 | + assert.assertEquals(options.jsxImportSource, undefined); |
| 144 | +}); |
| 145 | + |
| 146 | +Deno.test("EsbuildBuilderOptions basePath configuration", () => { |
| 147 | + const options: EsbuildBuilderOptions = { |
| 148 | + buildID: "test", |
| 149 | + entrypoints: {}, |
| 150 | + dev: false, |
| 151 | + configPath: "./deno.json", |
| 152 | + target: "es2022", |
| 153 | + absoluteWorkingDir: "/test", |
| 154 | + basePath: "/static/assets", |
| 155 | + }; |
| 156 | + |
| 157 | + assert.assertEquals(options.basePath, "/static/assets"); |
| 158 | +}); |
| 159 | + |
| 160 | +Deno.test("EsbuildBuilderOptions dev flag affects behavior", () => { |
| 161 | + const devOptions: EsbuildBuilderOptions = { |
| 162 | + buildID: "test", |
| 163 | + entrypoints: {}, |
| 164 | + dev: true, |
| 165 | + configPath: "./deno.json", |
| 166 | + target: "es2022", |
| 167 | + absoluteWorkingDir: "/test", |
| 168 | + }; |
| 169 | + |
| 170 | + const prodOptions: EsbuildBuilderOptions = { |
| 171 | + buildID: "test", |
| 172 | + entrypoints: {}, |
| 173 | + dev: false, |
| 174 | + configPath: "./deno.json", |
| 175 | + target: "es2022", |
| 176 | + absoluteWorkingDir: "/test", |
| 177 | + }; |
| 178 | + |
| 179 | + assert.assertEquals(devOptions.dev, true); |
| 180 | + assert.assertEquals(prodOptions.dev, false); |
| 181 | +}); |
| 182 | + |
| 183 | +Deno.test("EsbuildBuilder state is readonly", () => { |
| 184 | + const options: EsbuildBuilderOptions = { |
| 185 | + buildID: "test", |
| 186 | + entrypoints: {}, |
| 187 | + dev: false, |
| 188 | + configPath: "./deno.json", |
| 189 | + target: "es2022", |
| 190 | + absoluteWorkingDir: "/test", |
| 191 | + }; |
| 192 | + |
| 193 | + const state = createEsbuildBuilderState(options); |
| 194 | + const builder = new EsbuildBuilder(state); |
| 195 | + |
| 196 | + // Verify state is readonly by checking if it's the same reference |
| 197 | + assert.assertEquals(builder.state, state); |
| 198 | + |
| 199 | + // Try to verify readonly nature (TypeScript would catch this, but test the runtime behavior) |
| 200 | + const originalState = builder.state; |
| 201 | + assert.assertEquals(builder.state, originalState); |
| 202 | +}); |
0 commit comments