Repository: typeorm/typeorm
File: test/unit/util/import-utils.ts (TypeScript)

Code before:
        const readFileStub = sinon.stub(fsAsync.promises, "readFile")

        assert.equal(
            statStub.callCount,
            0,
            "stat should not be called before importOrRequireFile",
        )
        assert.equal(
            readFileStub.callCount,
            0,
            "readFile should not be called before importOrRequireFile",
        )

        const filePath1 = path.join(__dirname, "file1.js")
        const filePath2 = path.join(__dirname, "file2.js")
        const filePath3 = path.join(__dirname, "file3.js")

        await fs.writeFile(filePath1, "", "utf8")
        await fs.writeFile(filePath2, "", "utf8")
        await fs.writeFile(filePath3, "", "utf8")

        // Trigger the first import to create the cache
        await importOrRequireFile(filePath1)

        // Get the number of calls to stat and readFile after the first import
        const numberOfStatCalls = statStub.callCount
        const numberOfReadFileCalls = readFileStub.callCount

        // Trigger next imports to check if cache is used
        await importOrRequireFile(filePath2)
        await importOrRequireFile(filePath3)

        assert.equal(
            statStub.callCount,
            numberOfStatCalls,
            "stat should be called only during the first import",
        )
        assert.equal(
            readFileStub.callCount,
            numberOfReadFileCalls,
            "readFile should be called only during the first import",
        )

        // Clean up test files
        await fs.unlink(filePath1)
        await fs.unlink(filePath2)
        await fs.unlink(filePath3)

        sinon.restore()
    })
})

Review comment (suggestion):
I would also add a check of the actual number of calls to see that the call is actually happening before caching. e.g. not store a statStub.callCount into a variable but use a constant number instead.

Code after:
        const packageJsonPath = path.join(__dirname, "package.json")
        const packageJsonAlreadyExisted = fsAsync.existsSync(packageJsonPath)
        if (!packageJsonAlreadyExisted) {
            await fs.writeFile(
                packageJsonPath,
                JSON.stringify({ name: "test-package" }),
                "utf8",
            )
        }

        const statSpy = sinon.spy(fsAsync.promises, "stat")
        const readFileSpy = sinon.spy(fsAsync.promises, "readFile")

        assert.equal(
            statSpy.callCount,
            0,
            "stat should not be called before importOrRequireFile",
        )
        assert.equal(
            readFileSpy.callCount,
            0,
            "readFile should not be called before importOrRequireFile",
        )

        const filePath1 = path.join(__dirname, "file1.js")
        const filePath2 = path.join(__dirname, "file2.js")
        const filePath3 = path.join(__dirname, "file3.js")

        await fs.writeFile(filePath1, "", "utf8")
        await fs.writeFile(filePath2, "", "utf8")
        await fs.writeFile(filePath3, "", "utf8")

        // Trigger the first import to create the cache
        await importOrRequireFile(filePath1)

        // Get the number of calls to stat and readFile after the first import
        const numberOfStatCalls = statSpy.callCount
        const numberOfReadFileCalls = readFileSpy.callCount

        assert.equal(
            numberOfStatCalls,
            1,
            "stat should be called for the first import",
        )

        assert.equal(
            numberOfReadFileCalls,
            1,
            "readFile should be called for the first import",
        )