test: use built-in wait function and fix wait times to avoid flaky tests (#11812)

This commit is contained in:
Lucian Mocanu 2025-11-29 10:25:39 +01:00 committed by GitHub
parent 546192767d
commit 835647ac92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 64 additions and 71 deletions

View File

@ -34,6 +34,7 @@ jobs:
src-or-tests: &src-or-tests
- *src
- test/**/*.ts
- .github/workflows/test/**/*
- .github/workflows/test*.yml
- .mocharc.json

View File

@ -1,11 +1,11 @@
import { expect } from "chai"
import "reflect-metadata"
import { scheduler } from "timers/promises"
import { DataSource } from "../../../../src"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { Post } from "./entity/Post"
@ -81,7 +81,7 @@ describe("column kinds > create date column", () => {
})
// wait a second
await sleep(1000)
await scheduler.wait(1010)
// create post once again
post.title = "Updated Title"

View File

@ -1,11 +1,11 @@
import { expect } from "chai"
import "reflect-metadata"
import { scheduler } from "timers/promises"
import { DataSource } from "../../../../src"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { Post } from "./entity/Post"
@ -109,7 +109,7 @@ describe("column kinds > update date column", () => {
})
// wait a second
await sleep(2000)
await scheduler.wait(1010)
// update post once again
post.title = "Updated Title"
@ -120,9 +120,9 @@ describe("column kinds > update date column", () => {
await postRepository.findOneByOrFail({
id: post.id,
})
expect(loadedPostAfterUpdate.updatedAt.getTime()).to.be.not.eql(
loadedPostBeforeUpdate.updatedAt.getTime(),
)
expect(
loadedPostAfterUpdate.updatedAt.getTime(),
).to.be.greaterThan(loadedPostBeforeUpdate.updatedAt.getTime())
}),
))

View File

@ -5,7 +5,6 @@ import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { Post } from "./entity/Post"
@ -98,9 +97,6 @@ describe("column kinds > version column", () => {
post.title = "Post"
await postRepository.save(post)
// wait a second
await sleep(1000)
// update post once again
post.title = "Updated Title"
await postRepository.save(post)

View File

@ -1,15 +1,15 @@
import "reflect-metadata"
import { expect } from "chai"
import "reflect-metadata"
import { scheduler } from "timers/promises"
import { DataSource } from "../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/DataSource"
import { Address } from "./entity/Address"
import { User } from "./entity/User"
import { MockQueryResultCache } from "./provider/MockQueryResultCache"
import { Address } from "./entity/Address"
describe("custom cache provider", () => {
let connections: DataSource[]
@ -101,7 +101,7 @@ describe("custom cache provider", () => {
expect(users3.length).to.be.equal(1)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection
@ -179,7 +179,7 @@ describe("custom cache provider", () => {
expect(users3.length).to.be.equal(1)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection
@ -249,7 +249,7 @@ describe("custom cache provider", () => {
expect(users2.length).to.be.equal(2)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// but with cache enabled it must not return newly inserted entity since cache is not expired yet
const users3 = await connection
@ -263,7 +263,7 @@ describe("custom cache provider", () => {
expect(users3.length).to.be.equal(1)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection
@ -389,7 +389,7 @@ describe("custom cache provider", () => {
expect(users3).to.be.equal(1)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection

View File

@ -16,8 +16,8 @@ describe("driver > mysql > connection options > enableQueryTimeout", () => {
dropSchema: true,
enabledDrivers: ["mariadb", "mysql"],
}
const timeoutMs = 150
const longQueryTimeSec = 0.2
const timeoutMs = 200
const longQueryTimeSec = 0.3
const shortQueryTimeSec = 0.005
describe("when enableQueryTimeout is true", () => {

View File

@ -1,15 +1,15 @@
import "reflect-metadata"
import { expect } from "chai"
import { Post } from "./entity/Post"
import { Counters } from "./entity/Counters"
import "reflect-metadata"
import { scheduler } from "timers/promises"
import { DataSource } from "../../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { Subcounters } from "../embedded-many-to-one-case2/entity/Subcounters"
import { Counters } from "./entity/Counters"
import { Post } from "./entity/Post"
describe("embedded > embedded-with-special-columns", () => {
let connections: DataSource[]
@ -106,7 +106,9 @@ describe("embedded > embedded-with-special-columns", () => {
loadedPost!.title = "About cars #2"
await sleep(1000)
// wait a second
await scheduler.wait(1010)
await connection.getRepository(Post).save(loadedPost!)
loadedPost = await connection.manager

View File

@ -1,12 +1,12 @@
import "reflect-metadata"
import { expect } from "chai"
import "reflect-metadata"
import { scheduler } from "timers/promises"
import { DataSource } from "../../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { DataSource } from "../../../../src/data-source/DataSource"
import { User } from "./entity/User"
describe("query builder > cache", () => {
@ -80,7 +80,7 @@ describe("query builder > cache", () => {
expect(users3.length).to.be.equal(1)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection
@ -154,7 +154,7 @@ describe("query builder > cache", () => {
expect(users3.length).to.be.equal(1)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection
@ -220,7 +220,7 @@ describe("query builder > cache", () => {
expect(users2.length).to.be.equal(2)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// but with cache enabled it must not return newly inserted entity since cache is not expired yet
const users3 = await connection
@ -234,7 +234,7 @@ describe("query builder > cache", () => {
expect(users3.length).to.be.equal(1)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection
@ -302,7 +302,7 @@ describe("query builder > cache", () => {
expect(users3).to.be.equal(1)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection

View File

@ -1,11 +1,11 @@
import "reflect-metadata"
import { scheduler } from "timers/promises"
import { DataSource } from "../../../../src/index"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { DataSource } from "../../../../src/index"
import { Account } from "./entity/Account"
import { Person } from "./entity/Person"
@ -40,7 +40,7 @@ describe("query builder > time-travel-query", () => {
await repository.save(account)
// wait for 5 seconds
await sleep(5000)
await scheduler.wait(5000)
// update account balance
account.balance = 200
@ -72,7 +72,7 @@ describe("query builder > time-travel-query", () => {
await repository.save(account)
// wait for 2 seconds
await sleep(2000)
await scheduler.wait(2000)
// update account balance
account.balance = 200
@ -106,7 +106,7 @@ describe("query builder > time-travel-query", () => {
}
// wait for 2 seconds
await sleep(2000)
await scheduler.wait(2000)
let accounts = await repository
.createQueryBuilder("account")
@ -164,7 +164,7 @@ describe("query builder > time-travel-query", () => {
}
// wait for 2 seconds
await sleep(2000)
await scheduler.wait(2000)
const accounts = await accountRepository
.createQueryBuilder("account")
@ -224,7 +224,7 @@ describe("query builder > time-travel-query", () => {
}
// wait for 2 seconds
await sleep(2000)
await scheduler.wait(2000)
const accounts = await accountRepository
.createQueryBuilder("account")

View File

@ -1,28 +1,28 @@
import "reflect-metadata"
import { scheduler } from "timers/promises"
import { EntitySchema, Like, Repository, TypeORMError } from "../../../../src"
import { DeepPartial } from "../../../../src/common/DeepPartial"
import { DataSource } from "../../../../src/data-source/DataSource"
import { QueryBuilder } from "../../../../src/query-builder/QueryBuilder"
import { UpsertOptions } from "../../../../src/repository/UpsertOptions"
import "../../../utils/test-setup"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { DataSource } from "../../../../src/data-source/DataSource"
import { Blog } from "./entity/Blog"
import { Category } from "./entity/Category"
import { EmbeddedUQEntity } from "./entity/EmbeddedUQEntity"
import { ExternalIdPrimaryKeyEntity } from "./entity/ExternalIdPrimaryKeyEntity"
import { OneToOneRelationEntity } from "./entity/OneToOneRelation"
import { Post } from "./entity/Post"
import { QueryBuilder } from "../../../../src/query-builder/QueryBuilder"
import { User } from "./model/User"
import { RelationAsPrimaryKey } from "./entity/RelationAsPrimaryKey"
import { TwoUniqueColumnsEntity } from "./entity/TwoUniqueColumns"
import questionSchema from "./model-schema/QuestionSchema"
import userSchema from "./model-schema/UserSchema"
import { Question } from "./model/Question"
import { Blog } from "./entity/Blog"
import { Category } from "./entity/Category"
import { DeepPartial } from "../../../../src/common/DeepPartial"
import { EntitySchema, Like, Repository, TypeORMError } from "../../../../src"
import { ExternalIdPrimaryKeyEntity } from "./entity/ExternalIdPrimaryKeyEntity"
import { EmbeddedUQEntity } from "./entity/EmbeddedUQEntity"
import { RelationAsPrimaryKey } from "./entity/RelationAsPrimaryKey"
import { TwoUniqueColumnsEntity } from "./entity/TwoUniqueColumns"
import { OneToOneRelationEntity } from "./entity/OneToOneRelation"
import { UpsertOptions } from "../../../../src/repository/UpsertOptions"
import { User } from "./model/User"
describe("repository > basic methods", () => {
const UserEntity = new EntitySchema<any>(userSchema as any)
@ -467,7 +467,7 @@ describe("repository > basic methods", () => {
initial.title.should.be.equal("Post title initial")
await sleep(1000)
await scheduler.wait(100)
// update post with externalId
await postRepository.upsert(

View File

@ -1,19 +1,19 @@
import "reflect-metadata"
import fs from "fs/promises"
import { expect } from "chai"
import fs from "fs/promises"
import "reflect-metadata"
import sinon from "sinon"
import { scheduler } from "timers/promises"
import { FileLogger } from "../../../../src"
import { DataSource } from "../../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
sleep,
} from "../../../utils/test-utils"
import { DataSource } from "../../../../src/data-source/DataSource"
import { User } from "./entity/User"
import { Category } from "./entity/Category"
import { Post } from "./entity/Post"
import { Photo } from "./entity/Photo"
import { FileLogger } from "../../../../src"
import { Post } from "./entity/Post"
import { User } from "./entity/User"
describe("repository > find options", () => {
let connections: DataSource[]
@ -328,7 +328,7 @@ describe("repository > find options > cache", () => {
expect(users3.length).to.be.equal(3)
// give some time for cache to expire
await sleep(1000)
await scheduler.wait(1010)
// now, when our cache has expired we check if we have new user inserted even with cache enabled
const users4 = await connection

View File

@ -522,12 +522,6 @@ export function generateRandomText(length: number): string {
return text
}
export function sleep(ms: number): Promise<void> {
return new Promise<void>((ok) => {
setTimeout(ok, ms)
})
}
/**
* Creates typeorm service table for storing user defined Views and generate columns.
*/