@geekmidas/testkit 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PostgresKyselyMigrator.spec +471 -0
- package/dist/Factory.mjs +1 -1
- package/dist/{KyselyFactory-DiiWtMYe.cjs → KyselyFactory-BGvSMLtd.cjs} +11 -12
- package/dist/{KyselyFactory-DZewtWtJ.mjs → KyselyFactory-ionH4gvk.mjs} +12 -13
- package/dist/KyselyFactory.cjs +2 -1
- package/dist/KyselyFactory.mjs +3 -2
- package/dist/{ObjectionFactory-MAf2m8LI.mjs → ObjectionFactory-CFrtXe7i.mjs} +1 -1
- package/dist/ObjectionFactory.cjs +1 -1
- package/dist/ObjectionFactory.mjs +2 -2
- package/dist/{PostgresKyselyMigrator-ChMJpPrQ.mjs → PostgresKyselyMigrator-CbtiZgfI.mjs} +1 -1
- package/dist/{PostgresKyselyMigrator-rY3hO_-1.cjs → PostgresKyselyMigrator-Cxf2Dp9y.cjs} +3 -2
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/{PostgresMigrator-BJ2-5A_b.cjs → PostgresMigrator-eqyAFSf-.cjs} +2 -39
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/VitestKyselyTransactionIsolator-DXjWQtDN.mjs +12 -0
- package/dist/VitestKyselyTransactionIsolator-Dh2AgJDd.cjs +17 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +5 -0
- package/dist/VitestKyselyTransactionIsolator.mjs +5 -0
- package/dist/VitestTransactionIsolator-pLwsDo_A.mjs +40 -0
- package/dist/VitestTransactionIsolator-zK5NJ7DQ.cjs +51 -0
- package/dist/VitestTransactionIsolator.cjs +5 -0
- package/dist/VitestTransactionIsolator.mjs +4 -0
- package/dist/__tests__/Factory.spec.cjs +139 -0
- package/dist/__tests__/Factory.spec.mjs +139 -0
- package/dist/__tests__/KyselyFactory.spec.cjs +221 -15008
- package/dist/__tests__/KyselyFactory.spec.mjs +220 -15034
- package/dist/__tests__/ObjectionFactory.spec.cjs +387 -0
- package/dist/__tests__/ObjectionFactory.spec.mjs +386 -0
- package/dist/__tests__/PostgresMigrator.spec.cjs +257 -0
- package/dist/__tests__/PostgresMigrator.spec.mjs +256 -0
- package/dist/__tests__/faker.spec.cjs +115 -0
- package/dist/__tests__/faker.spec.mjs +115 -0
- package/dist/__tests__/integration.spec.cjs +279 -0
- package/dist/__tests__/integration.spec.mjs +279 -0
- package/dist/chunk-DWy1uDak.cjs +39 -0
- package/dist/dist-BM2KvLG1.mjs +5618 -0
- package/dist/dist-DE3gAxQI.cjs +5736 -0
- package/dist/example.cjs +2 -1
- package/dist/example.mjs +3 -2
- package/dist/faker-cGCFcrj2.mjs +85 -0
- package/dist/faker-h6CkRloU.cjs +121 -0
- package/dist/faker.cjs +8 -0
- package/dist/faker.mjs +3 -0
- package/dist/helpers-BnARb5Ap.mjs +19 -0
- package/dist/helpers-C2NH7xcz.cjs +135 -0
- package/dist/helpers-C_RZk04R.cjs +31 -0
- package/dist/helpers-CukcFAU9.mjs +111 -0
- package/dist/helpers.cjs +7 -0
- package/dist/helpers.mjs +6 -0
- package/dist/kysely.cjs +16 -4
- package/dist/kysely.mjs +16 -5
- package/dist/objection.cjs +1 -1
- package/dist/objection.mjs +2 -2
- package/dist/vi.bdSIJ99Y-BgRxGeO2.mjs +9382 -0
- package/dist/vi.bdSIJ99Y-CFuzUeY6.cjs +9393 -0
- package/package.json +7 -2
- package/src/Factory.ts +3 -1
- package/src/KyselyFactory.ts +30 -36
- package/src/VitestKyselyTransactionIsolator.ts +23 -0
- package/src/VitestTransactionIsolator.ts +70 -0
- package/src/__tests__/Factory.spec.ts +164 -0
- package/src/__tests__/KyselyFactory.spec.ts +432 -64
- package/src/__tests__/ObjectionFactory.spec.ts +532 -0
- package/src/__tests__/PostgresMigrator.spec.ts +366 -0
- package/src/__tests__/faker.spec.ts +142 -0
- package/src/__tests__/integration.spec.ts +442 -0
- package/src/faker.ts +112 -0
- package/src/helpers.ts +28 -0
- package/src/kysely.ts +14 -0
- package/test/globalSetup.ts +41 -40
- package/test/helpers.ts +273 -0
- /package/dist/{Factory-DlzMkMzb.mjs → Factory-D52Lsc6Z.mjs} +0 -0
- /package/dist/{ObjectionFactory-DeFYWbzt.cjs → ObjectionFactory-BlkzSEqo.cjs} +0 -0
- /package/dist/{PostgresMigrator-BKaNTth5.mjs → PostgresMigrator-DqeuPy-e.mjs} +0 -0
- /package/dist/{magic-string.es-CxbtJGk_.mjs → magic-string.es-C6yzoryu.mjs} +0 -0
- /package/dist/{magic-string.es-KiPEzMtt.cjs → magic-string.es-jdtJrR0A.cjs} +0 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, it } from "../dist-BM2KvLG1.mjs";
|
|
2
|
+
import { PostgresMigrator } from "../PostgresMigrator-DqeuPy-e.mjs";
|
|
3
|
+
import { globalExpect, vi } from "../vi.bdSIJ99Y-BgRxGeO2.mjs";
|
|
4
|
+
import { createTestDatabase } from "../helpers-CukcFAU9.mjs";
|
|
5
|
+
import { Client } from "pg";
|
|
6
|
+
|
|
7
|
+
//#region src/__tests__/PostgresMigrator.spec.ts
|
|
8
|
+
var TestPostgresMigrator = class extends PostgresMigrator {
|
|
9
|
+
migrateCalled = false;
|
|
10
|
+
migrateError;
|
|
11
|
+
customMigrations = [];
|
|
12
|
+
async migrate() {
|
|
13
|
+
this.migrateCalled = true;
|
|
14
|
+
if (this.migrateError) throw this.migrateError;
|
|
15
|
+
for (const migration of this.customMigrations) await migration();
|
|
16
|
+
}
|
|
17
|
+
addMigration(migration) {
|
|
18
|
+
this.customMigrations.push(migration);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
describe("PostgresMigrator", () => {
|
|
22
|
+
let testDbName;
|
|
23
|
+
let cleanupDb;
|
|
24
|
+
let consoleSpy;
|
|
25
|
+
beforeAll(async () => {
|
|
26
|
+
testDbName = `test_postgres_migrator_${Date.now()}`;
|
|
27
|
+
cleanupDb = await createTestDatabase(testDbName);
|
|
28
|
+
});
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
31
|
+
});
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
consoleSpy.mockRestore();
|
|
34
|
+
});
|
|
35
|
+
afterAll(async () => {
|
|
36
|
+
await cleanupDb();
|
|
37
|
+
});
|
|
38
|
+
describe("constructor", () => {
|
|
39
|
+
it("should create a PostgresMigrator instance", () => {
|
|
40
|
+
const migrator = new TestPostgresMigrator(`postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`);
|
|
41
|
+
globalExpect(migrator).toBeInstanceOf(PostgresMigrator);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe("start method", () => {
|
|
45
|
+
it("should create database, migrate, and return cleanup function", async () => {
|
|
46
|
+
const newDbName = `test_start_${Date.now()}`;
|
|
47
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
|
|
48
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
49
|
+
let migrationRan = false;
|
|
50
|
+
migrator.addMigration(async () => {
|
|
51
|
+
migrationRan = true;
|
|
52
|
+
});
|
|
53
|
+
const cleanup = await migrator.start();
|
|
54
|
+
globalExpect(migrator.migrateCalled).toBe(true);
|
|
55
|
+
globalExpect(migrationRan).toBe(true);
|
|
56
|
+
globalExpect(consoleSpy).toHaveBeenCalledWith(`Migrating database: ${newDbName}`);
|
|
57
|
+
globalExpect(typeof cleanup).toBe("function");
|
|
58
|
+
await cleanup();
|
|
59
|
+
});
|
|
60
|
+
it("should handle existing database", async () => {
|
|
61
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
|
|
62
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
63
|
+
let migrationRan = false;
|
|
64
|
+
migrator.addMigration(async () => {
|
|
65
|
+
migrationRan = true;
|
|
66
|
+
});
|
|
67
|
+
const cleanup = await migrator.start();
|
|
68
|
+
globalExpect(migrator.migrateCalled).toBe(true);
|
|
69
|
+
globalExpect(migrationRan).toBe(true);
|
|
70
|
+
globalExpect(typeof cleanup).toBe("function");
|
|
71
|
+
});
|
|
72
|
+
it("should handle URI with query parameters", async () => {
|
|
73
|
+
const queryDbName = `test_query_params_${Date.now()}`;
|
|
74
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${queryDbName}?ssl=false&timeout=30`;
|
|
75
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
76
|
+
const cleanup = await migrator.start();
|
|
77
|
+
globalExpect(migrator.migrateCalled).toBe(true);
|
|
78
|
+
globalExpect(typeof cleanup).toBe("function");
|
|
79
|
+
await cleanup();
|
|
80
|
+
});
|
|
81
|
+
it("should clean up connections even if migration fails", async () => {
|
|
82
|
+
const failDbName = `test_fail_${Date.now()}`;
|
|
83
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${failDbName}`;
|
|
84
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
85
|
+
migrator.migrateError = new Error("Migration failed");
|
|
86
|
+
await globalExpect(migrator.start()).rejects.toThrow("Migration failed");
|
|
87
|
+
globalExpect(migrator.migrateCalled).toBe(true);
|
|
88
|
+
const cleanupClient = new Client({
|
|
89
|
+
host: "localhost",
|
|
90
|
+
port: 5432,
|
|
91
|
+
user: "geekmidas",
|
|
92
|
+
password: "geekmidas",
|
|
93
|
+
database: "postgres"
|
|
94
|
+
});
|
|
95
|
+
try {
|
|
96
|
+
await cleanupClient.connect();
|
|
97
|
+
await cleanupClient.query(`DROP DATABASE IF EXISTS "${failDbName}"`);
|
|
98
|
+
} finally {
|
|
99
|
+
await cleanupClient.end();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
it("should return cleanup function that drops database", async () => {
|
|
103
|
+
const cleanupDbName = `test_cleanup_${Date.now()}`;
|
|
104
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${cleanupDbName}`;
|
|
105
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
106
|
+
const cleanup = await migrator.start();
|
|
107
|
+
globalExpect(migrator.migrateCalled).toBe(true);
|
|
108
|
+
globalExpect(typeof cleanup).toBe("function");
|
|
109
|
+
const checkClient = new Client({
|
|
110
|
+
host: "localhost",
|
|
111
|
+
port: 5432,
|
|
112
|
+
user: "geekmidas",
|
|
113
|
+
password: "geekmidas",
|
|
114
|
+
database: "postgres"
|
|
115
|
+
});
|
|
116
|
+
await checkClient.connect();
|
|
117
|
+
const beforeResult = await checkClient.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = $1`, [cleanupDbName]);
|
|
118
|
+
globalExpect(beforeResult.rowCount).toBe(1);
|
|
119
|
+
await checkClient.end();
|
|
120
|
+
await cleanup();
|
|
121
|
+
const checkClient2 = new Client({
|
|
122
|
+
host: "localhost",
|
|
123
|
+
port: 5432,
|
|
124
|
+
user: "geekmidas",
|
|
125
|
+
password: "geekmidas",
|
|
126
|
+
database: "postgres"
|
|
127
|
+
});
|
|
128
|
+
await checkClient2.connect();
|
|
129
|
+
const afterResult = await checkClient2.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = $1`, [cleanupDbName]);
|
|
130
|
+
globalExpect(afterResult.rowCount).toBe(0);
|
|
131
|
+
await checkClient2.end();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
describe("database creation", () => {
|
|
135
|
+
it("should handle connection errors gracefully", async () => {
|
|
136
|
+
const badDbName = `test_bad_connection_${Date.now()}`;
|
|
137
|
+
const uri = `postgresql://invalid_user:invalid_pass@localhost:5432/${badDbName}`;
|
|
138
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
139
|
+
await globalExpect(migrator.start()).rejects.toThrow();
|
|
140
|
+
});
|
|
141
|
+
it("should handle invalid database names", async () => {
|
|
142
|
+
const invalidDbName = "test-invalid-db-name!";
|
|
143
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${invalidDbName}`;
|
|
144
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
145
|
+
await globalExpect(migrator.start).rejects.toThrow();
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
describe("URI parsing", () => {
|
|
149
|
+
it("should parse different URI formats correctly", async () => {
|
|
150
|
+
const testDbName$1 = `test_uri_parsing_${Date.now()}`;
|
|
151
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName$1}`;
|
|
152
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
153
|
+
const cleanup = await migrator.start();
|
|
154
|
+
globalExpect(migrator.migrateCalled).toBe(true);
|
|
155
|
+
globalExpect(typeof cleanup).toBe("function");
|
|
156
|
+
await cleanup();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
describe("error handling", () => {
|
|
160
|
+
it("should propagate migration errors", async () => {
|
|
161
|
+
const errorDbName = `test_migration_error_${Date.now()}`;
|
|
162
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${errorDbName}`;
|
|
163
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
164
|
+
const migrationError = new Error("Custom migration error");
|
|
165
|
+
migrator.migrateError = migrationError;
|
|
166
|
+
await globalExpect(migrator.start()).rejects.toThrow("Custom migration error");
|
|
167
|
+
const cleanupClient = new Client({
|
|
168
|
+
host: "localhost",
|
|
169
|
+
port: 5432,
|
|
170
|
+
user: "geekmidas",
|
|
171
|
+
password: "geekmidas",
|
|
172
|
+
database: "postgres"
|
|
173
|
+
});
|
|
174
|
+
try {
|
|
175
|
+
await cleanupClient.connect();
|
|
176
|
+
await cleanupClient.query(`DROP DATABASE IF EXISTS "${errorDbName}"`);
|
|
177
|
+
} finally {
|
|
178
|
+
await cleanupClient.end();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
it("should handle cleanup errors gracefully", async () => {
|
|
182
|
+
const cleanupErrorDbName = `test_cleanup_error_${Date.now()}`;
|
|
183
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${cleanupErrorDbName}`;
|
|
184
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
185
|
+
const cleanup = await migrator.start();
|
|
186
|
+
const adminClient = new Client({
|
|
187
|
+
host: "localhost",
|
|
188
|
+
port: 5432,
|
|
189
|
+
user: "geekmidas",
|
|
190
|
+
password: "geekmidas",
|
|
191
|
+
database: "postgres"
|
|
192
|
+
});
|
|
193
|
+
await adminClient.connect();
|
|
194
|
+
await adminClient.query(`DROP DATABASE IF EXISTS "${cleanupErrorDbName}"`);
|
|
195
|
+
await adminClient.end();
|
|
196
|
+
await globalExpect(cleanup()).rejects.toThrow();
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
describe("abstract method", () => {
|
|
200
|
+
it("should require concrete implementation of migrate method", () => {
|
|
201
|
+
const migrator = new TestPostgresMigrator(`postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`);
|
|
202
|
+
globalExpect(typeof migrator.migrate).toBe("function");
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
describe("integration scenarios", () => {
|
|
206
|
+
it("should handle complete workflow", async () => {
|
|
207
|
+
const integrationDbName = `test_integration_${Date.now()}`;
|
|
208
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${integrationDbName}`;
|
|
209
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
210
|
+
migrator.addMigration(async () => {
|
|
211
|
+
const client = new Client({
|
|
212
|
+
host: "localhost",
|
|
213
|
+
port: 5432,
|
|
214
|
+
user: "geekmidas",
|
|
215
|
+
password: "geekmidas",
|
|
216
|
+
database: integrationDbName
|
|
217
|
+
});
|
|
218
|
+
await client.connect();
|
|
219
|
+
await client.query(`
|
|
220
|
+
CREATE TABLE IF NOT EXISTS test_table (
|
|
221
|
+
id SERIAL PRIMARY KEY,
|
|
222
|
+
name VARCHAR(255) NOT NULL
|
|
223
|
+
)
|
|
224
|
+
`);
|
|
225
|
+
await client.end();
|
|
226
|
+
});
|
|
227
|
+
const cleanup = await migrator.start();
|
|
228
|
+
globalExpect(migrator.migrateCalled).toBe(true);
|
|
229
|
+
globalExpect(consoleSpy).toHaveBeenCalledWith(`Migrating database: ${integrationDbName}`);
|
|
230
|
+
const verifyClient = new Client({
|
|
231
|
+
host: "localhost",
|
|
232
|
+
port: 5432,
|
|
233
|
+
user: "geekmidas",
|
|
234
|
+
password: "geekmidas",
|
|
235
|
+
database: integrationDbName
|
|
236
|
+
});
|
|
237
|
+
await verifyClient.connect();
|
|
238
|
+
const tableResult = await verifyClient.query(`
|
|
239
|
+
SELECT table_name FROM information_schema.tables
|
|
240
|
+
WHERE table_schema = 'public' AND table_name = 'test_table'
|
|
241
|
+
`);
|
|
242
|
+
globalExpect(tableResult.rowCount).toBe(1);
|
|
243
|
+
await verifyClient.end();
|
|
244
|
+
await cleanup();
|
|
245
|
+
});
|
|
246
|
+
it("should handle database that already exists and cleanup", async () => {
|
|
247
|
+
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
|
|
248
|
+
const migrator = new TestPostgresMigrator(uri);
|
|
249
|
+
const cleanup = await migrator.start();
|
|
250
|
+
globalExpect(migrator.migrateCalled).toBe(true);
|
|
251
|
+
globalExpect(typeof cleanup).toBe("function");
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const require_faker = require('../faker-h6CkRloU.cjs');
|
|
2
|
+
const require_dist = require('../dist-DE3gAxQI.cjs');
|
|
3
|
+
const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-CFuzUeY6.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/__tests__/faker.spec.ts
|
|
6
|
+
require_dist.describe("faker", () => {
|
|
7
|
+
require_dist.describe("sequence", () => {
|
|
8
|
+
require_dist.beforeEach(() => {
|
|
9
|
+
require_faker.faker.resetAllSequences();
|
|
10
|
+
});
|
|
11
|
+
require_dist.it("should start from 1 for a new sequence", () => {
|
|
12
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence()).toBe(1);
|
|
13
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("custom")).toBe(1);
|
|
14
|
+
});
|
|
15
|
+
require_dist.it("should increment on each call", () => {
|
|
16
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence()).toBe(1);
|
|
17
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence()).toBe(2);
|
|
18
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence()).toBe(3);
|
|
19
|
+
});
|
|
20
|
+
require_dist.it("should maintain separate counters for different names", () => {
|
|
21
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("users")).toBe(1);
|
|
22
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("posts")).toBe(1);
|
|
23
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("users")).toBe(2);
|
|
24
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("posts")).toBe(2);
|
|
25
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("users")).toBe(3);
|
|
26
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("posts")).toBe(3);
|
|
27
|
+
});
|
|
28
|
+
require_dist.it("should handle concurrent-like sequential calls", () => {
|
|
29
|
+
const results = [];
|
|
30
|
+
for (let i = 0; i < 100; i++) results.push(require_faker.faker.sequence("concurrent"));
|
|
31
|
+
require_vi_bdSIJ99Y.globalExpect(results).toEqual(Array.from({ length: 100 }, (_, i) => i + 1));
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
require_dist.describe("resetSequence", () => {
|
|
35
|
+
require_dist.beforeEach(() => {
|
|
36
|
+
require_faker.faker.resetAllSequences();
|
|
37
|
+
});
|
|
38
|
+
require_dist.it("should reset a specific sequence to 0", () => {
|
|
39
|
+
require_faker.faker.sequence("test");
|
|
40
|
+
require_faker.faker.sequence("test");
|
|
41
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("test")).toBe(3);
|
|
42
|
+
require_faker.faker.resetSequence("test");
|
|
43
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("test")).toBe(1);
|
|
44
|
+
});
|
|
45
|
+
require_dist.it("should reset a specific sequence to a custom value", () => {
|
|
46
|
+
require_faker.faker.sequence("test");
|
|
47
|
+
require_faker.faker.resetSequence("test", 10);
|
|
48
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("test")).toBe(11);
|
|
49
|
+
});
|
|
50
|
+
require_dist.it("should create a new sequence if it does not exist", () => {
|
|
51
|
+
require_faker.faker.resetSequence("new", 5);
|
|
52
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("new")).toBe(6);
|
|
53
|
+
});
|
|
54
|
+
require_dist.it("should not affect other sequences", () => {
|
|
55
|
+
require_faker.faker.sequence("test1");
|
|
56
|
+
require_faker.faker.sequence("test1");
|
|
57
|
+
require_faker.faker.sequence("test2");
|
|
58
|
+
require_faker.faker.resetSequence("test1");
|
|
59
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("test1")).toBe(1);
|
|
60
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("test2")).toBe(2);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
require_dist.describe("resetAllSequences", () => {
|
|
64
|
+
require_dist.it("should reset all sequences", () => {
|
|
65
|
+
require_faker.faker.sequence("test1");
|
|
66
|
+
require_faker.faker.sequence("test1");
|
|
67
|
+
require_faker.faker.sequence("test2");
|
|
68
|
+
require_faker.faker.sequence("test2");
|
|
69
|
+
require_faker.faker.sequence("test2");
|
|
70
|
+
require_faker.faker.resetAllSequences();
|
|
71
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("test1")).toBe(1);
|
|
72
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence("test2")).toBe(1);
|
|
73
|
+
require_vi_bdSIJ99Y.globalExpect(require_faker.faker.sequence()).toBe(1);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
require_dist.describe("identifier", () => {
|
|
77
|
+
require_dist.beforeEach(() => {
|
|
78
|
+
require_faker.faker.resetAllSequences();
|
|
79
|
+
});
|
|
80
|
+
require_dist.it("should include sequence number in identifier", () => {
|
|
81
|
+
const id1 = require_faker.faker.identifier();
|
|
82
|
+
const id2 = require_faker.faker.identifier();
|
|
83
|
+
require_vi_bdSIJ99Y.globalExpect(id1).not.toBe(id2);
|
|
84
|
+
require_vi_bdSIJ99Y.globalExpect(id1).toMatch(/1$/);
|
|
85
|
+
require_vi_bdSIJ99Y.globalExpect(id2).toMatch(/2$/);
|
|
86
|
+
});
|
|
87
|
+
require_dist.it("should use custom suffix when provided", () => {
|
|
88
|
+
const id = require_faker.faker.identifier("customSuffix");
|
|
89
|
+
require_vi_bdSIJ99Y.globalExpect(id).toMatch(/\.customSuffix$/);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
require_dist.describe("timestamps", () => {
|
|
93
|
+
require_dist.it("should return createdAt and updatedAt dates", () => {
|
|
94
|
+
const { createdAt, updatedAt } = require_faker.faker.timestamps();
|
|
95
|
+
require_vi_bdSIJ99Y.globalExpect(createdAt).toBeInstanceOf(Date);
|
|
96
|
+
require_vi_bdSIJ99Y.globalExpect(updatedAt).toBeInstanceOf(Date);
|
|
97
|
+
require_vi_bdSIJ99Y.globalExpect(createdAt.getTime()).toBeLessThanOrEqual(updatedAt.getTime());
|
|
98
|
+
require_vi_bdSIJ99Y.globalExpect(updatedAt.getTime()).toBeLessThanOrEqual((/* @__PURE__ */ new Date()).getTime());
|
|
99
|
+
});
|
|
100
|
+
require_dist.it("should have milliseconds set to 0", () => {
|
|
101
|
+
const { createdAt, updatedAt } = require_faker.faker.timestamps();
|
|
102
|
+
require_vi_bdSIJ99Y.globalExpect(createdAt.getMilliseconds()).toBe(0);
|
|
103
|
+
require_vi_bdSIJ99Y.globalExpect(updatedAt.getMilliseconds()).toBe(0);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
require_dist.describe("price", () => {
|
|
107
|
+
require_dist.it("should return a number", () => {
|
|
108
|
+
const result = require_faker.faker.price();
|
|
109
|
+
require_vi_bdSIJ99Y.globalExpect(typeof result).toBe("number");
|
|
110
|
+
require_vi_bdSIJ99Y.globalExpect(result).toBeGreaterThan(0);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
//#endregion
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { beforeEach, describe, it } from "../dist-BM2KvLG1.mjs";
|
|
2
|
+
import { faker } from "../faker-cGCFcrj2.mjs";
|
|
3
|
+
import { globalExpect } from "../vi.bdSIJ99Y-BgRxGeO2.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/__tests__/faker.spec.ts
|
|
6
|
+
describe("faker", () => {
|
|
7
|
+
describe("sequence", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
faker.resetAllSequences();
|
|
10
|
+
});
|
|
11
|
+
it("should start from 1 for a new sequence", () => {
|
|
12
|
+
globalExpect(faker.sequence()).toBe(1);
|
|
13
|
+
globalExpect(faker.sequence("custom")).toBe(1);
|
|
14
|
+
});
|
|
15
|
+
it("should increment on each call", () => {
|
|
16
|
+
globalExpect(faker.sequence()).toBe(1);
|
|
17
|
+
globalExpect(faker.sequence()).toBe(2);
|
|
18
|
+
globalExpect(faker.sequence()).toBe(3);
|
|
19
|
+
});
|
|
20
|
+
it("should maintain separate counters for different names", () => {
|
|
21
|
+
globalExpect(faker.sequence("users")).toBe(1);
|
|
22
|
+
globalExpect(faker.sequence("posts")).toBe(1);
|
|
23
|
+
globalExpect(faker.sequence("users")).toBe(2);
|
|
24
|
+
globalExpect(faker.sequence("posts")).toBe(2);
|
|
25
|
+
globalExpect(faker.sequence("users")).toBe(3);
|
|
26
|
+
globalExpect(faker.sequence("posts")).toBe(3);
|
|
27
|
+
});
|
|
28
|
+
it("should handle concurrent-like sequential calls", () => {
|
|
29
|
+
const results = [];
|
|
30
|
+
for (let i = 0; i < 100; i++) results.push(faker.sequence("concurrent"));
|
|
31
|
+
globalExpect(results).toEqual(Array.from({ length: 100 }, (_, i) => i + 1));
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
describe("resetSequence", () => {
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
faker.resetAllSequences();
|
|
37
|
+
});
|
|
38
|
+
it("should reset a specific sequence to 0", () => {
|
|
39
|
+
faker.sequence("test");
|
|
40
|
+
faker.sequence("test");
|
|
41
|
+
globalExpect(faker.sequence("test")).toBe(3);
|
|
42
|
+
faker.resetSequence("test");
|
|
43
|
+
globalExpect(faker.sequence("test")).toBe(1);
|
|
44
|
+
});
|
|
45
|
+
it("should reset a specific sequence to a custom value", () => {
|
|
46
|
+
faker.sequence("test");
|
|
47
|
+
faker.resetSequence("test", 10);
|
|
48
|
+
globalExpect(faker.sequence("test")).toBe(11);
|
|
49
|
+
});
|
|
50
|
+
it("should create a new sequence if it does not exist", () => {
|
|
51
|
+
faker.resetSequence("new", 5);
|
|
52
|
+
globalExpect(faker.sequence("new")).toBe(6);
|
|
53
|
+
});
|
|
54
|
+
it("should not affect other sequences", () => {
|
|
55
|
+
faker.sequence("test1");
|
|
56
|
+
faker.sequence("test1");
|
|
57
|
+
faker.sequence("test2");
|
|
58
|
+
faker.resetSequence("test1");
|
|
59
|
+
globalExpect(faker.sequence("test1")).toBe(1);
|
|
60
|
+
globalExpect(faker.sequence("test2")).toBe(2);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
describe("resetAllSequences", () => {
|
|
64
|
+
it("should reset all sequences", () => {
|
|
65
|
+
faker.sequence("test1");
|
|
66
|
+
faker.sequence("test1");
|
|
67
|
+
faker.sequence("test2");
|
|
68
|
+
faker.sequence("test2");
|
|
69
|
+
faker.sequence("test2");
|
|
70
|
+
faker.resetAllSequences();
|
|
71
|
+
globalExpect(faker.sequence("test1")).toBe(1);
|
|
72
|
+
globalExpect(faker.sequence("test2")).toBe(1);
|
|
73
|
+
globalExpect(faker.sequence()).toBe(1);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe("identifier", () => {
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
faker.resetAllSequences();
|
|
79
|
+
});
|
|
80
|
+
it("should include sequence number in identifier", () => {
|
|
81
|
+
const id1 = faker.identifier();
|
|
82
|
+
const id2 = faker.identifier();
|
|
83
|
+
globalExpect(id1).not.toBe(id2);
|
|
84
|
+
globalExpect(id1).toMatch(/1$/);
|
|
85
|
+
globalExpect(id2).toMatch(/2$/);
|
|
86
|
+
});
|
|
87
|
+
it("should use custom suffix when provided", () => {
|
|
88
|
+
const id = faker.identifier("customSuffix");
|
|
89
|
+
globalExpect(id).toMatch(/\.customSuffix$/);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
describe("timestamps", () => {
|
|
93
|
+
it("should return createdAt and updatedAt dates", () => {
|
|
94
|
+
const { createdAt, updatedAt } = faker.timestamps();
|
|
95
|
+
globalExpect(createdAt).toBeInstanceOf(Date);
|
|
96
|
+
globalExpect(updatedAt).toBeInstanceOf(Date);
|
|
97
|
+
globalExpect(createdAt.getTime()).toBeLessThanOrEqual(updatedAt.getTime());
|
|
98
|
+
globalExpect(updatedAt.getTime()).toBeLessThanOrEqual((/* @__PURE__ */ new Date()).getTime());
|
|
99
|
+
});
|
|
100
|
+
it("should have milliseconds set to 0", () => {
|
|
101
|
+
const { createdAt, updatedAt } = faker.timestamps();
|
|
102
|
+
globalExpect(createdAt.getMilliseconds()).toBe(0);
|
|
103
|
+
globalExpect(updatedAt.getMilliseconds()).toBe(0);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe("price", () => {
|
|
107
|
+
it("should return a number", () => {
|
|
108
|
+
const result = faker.price();
|
|
109
|
+
globalExpect(typeof result).toBe("number");
|
|
110
|
+
globalExpect(result).toBeGreaterThan(0);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
//#endregion
|