-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisolated.test.ts
More file actions
136 lines (113 loc) · 3.95 KB
/
Copy pathisolated.test.ts
File metadata and controls
136 lines (113 loc) · 3.95 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* To isolate tests within a file, reset the Neon branch before each test
*
* This file contains a few ways to isolate tests within a file
*/
import { describe, beforeEach, expect, test } from "vitest";
import { neonTesting } from "./neon-testing";
import { neon } from "@neondatabase/serverless";
neonTesting();
/**
* Drop the Postgres schema before each test
*
* When you drop the schema you have to re-apply the database migrations/schema
* before each test
*/
describe("isolate tests by dropping schema", () => {
beforeEach(async () => {
const sql = neon(process.env.DATABASE_URL!);
await sql`DROP SCHEMA IF EXISTS public CASCADE`;
await sql`CREATE SCHEMA public`;
await sql`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)`;
});
/**
* This test is run twice and would fail if the database is shared between
* tests
*/
test.each([0, 1])("individual tests are isolated - %s", async () => {
const sql = neon(process.env.DATABASE_URL!);
expect(await sql`SELECT * FROM users`).toStrictEqual([]);
await sql`INSERT INTO users (name) VALUES ('Ellen Ripley')`;
expect(await sql`SELECT * FROM users`).toStrictEqual([
{ id: 1, name: "Ellen Ripley" },
]);
});
});
/**
* Drop the table before each test
*
* When you drop tables you have to re-apply the database migrations/schema
* before each test
*/
describe("isolate tests by dropping tables", () => {
beforeEach(async () => {
const sql = neon(process.env.DATABASE_URL!);
await sql`DROP TABLE IF EXISTS users`;
await sql`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)`;
await sql`DELETE FROM users`;
});
/**
* This test is run twice and would fail if the database is shared between
* tests
*/
test.each([0, 1])("individual tests are isolated - %s", async () => {
const sql = neon(process.env.DATABASE_URL!);
expect(await sql`SELECT * FROM users`).toStrictEqual([]);
await sql`INSERT INTO users (name) VALUES ('Ellen Ripley')`;
expect(await sql`SELECT * FROM users`).toStrictEqual([
{ id: 1, name: "Ellen Ripley" },
]);
});
});
/**
* Delete table rows before each test
*
* When you delete rows, the tables themselves are intact so there is no need to
* re-apply the database migrations/schema
*/
describe("isolate tests by deleting rows", () => {
beforeEach(async () => {
const sql = neon(process.env.DATABASE_URL!);
await sql`CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name TEXT)`;
await sql`DELETE FROM users`;
await sql`ALTER SEQUENCE users_id_seq RESTART WITH 1`;
});
/**
* This test is run twice and would fail if the database is shared between
* tests
*/
test.each([0, 1])("individual tests are isolated - %s", async () => {
const sql = neon(process.env.DATABASE_URL!);
expect(await sql`SELECT * FROM users`).toStrictEqual([]);
await sql`INSERT INTO users (name) VALUES ('Ellen Ripley')`;
expect(await sql`SELECT * FROM users`).toStrictEqual([
{ id: 1, name: "Ellen Ripley" },
]);
});
});
/**
* Truncate tables before each test
*
* When you truncate tables, the tables themselves are intact so there is no
* need to re-apply the database migrations/schema
*/
describe("isolate tests by truncating tables", () => {
beforeEach(async () => {
const sql = neon(process.env.DATABASE_URL!);
await sql`CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name TEXT)`;
await sql`TRUNCATE TABLE users`;
await sql`ALTER SEQUENCE users_id_seq RESTART WITH 1`;
});
/**
* This test is run twice and would fail if the database is shared between
* tests
*/
test.each([0, 1])("individual tests are isolated - %s", async () => {
const sql = neon(process.env.DATABASE_URL!);
expect(await sql`SELECT * FROM users`).toStrictEqual([]);
await sql`INSERT INTO users (name) VALUES ('Ellen Ripley')`;
expect(await sql`SELECT * FROM users`).toStrictEqual([
{ id: 1, name: "Ellen Ripley" },
]);
});
});