
Research
Security News
Malicious npm Packages Target BSC and Ethereum to Drain Crypto Wallets
Socket uncovered four malicious npm packages that exfiltrate up to 85% of a victim’s Ethereum or BSC wallet using obfuscated JavaScript.
@chax-at/transactional-prisma-testing
Advanced tools
Provides an easy way to execute database tests in a transaction that will be rolled back after each test for fast testing
This package provides an easy way to run test cases inside a single database transaction that will be rolled back after each test. This allows fast test execution while still providing the same source database state for each test. It also allows parallel test execution against the same database.
Install the package by running
npm i -D @chax-at/transactional-prisma-testing
Note that this will install the package as a dev dependency, intended to be used during tests only (remove the -D
if you want to use it outside of tests).
The following example for a NestJS project shows how this package can be used.
It is however possible to use this package with every testing framework, just check out the documentation below and adapt the code accordingly.
You can simply replace the PrismaService
with PrismaClient
if you are not using NestJS.
import { PrismaTestingHelper } from '@chax-at/transactional-prisma-testing';
// Cache for the PrismaTestingHelper. Only one PrismaTestingHelper should be instantiated per test runner (i.e. only one if your tests run sequentially).
let prismaTestingHelper: PrismaTestingHelper<PrismaService> | undefined;
// Saves the PrismaService that will be used during test cases. Will always execute queries on the currently active transaction.
let prismaService: PrismaService;
// This function must be called before every test
async function before(): Promise<void> {
if(prismaTestingHelper == null) {
// Initialize testing helper if it has not been initialized before
const originalPrismaService = new PrismaService();
// Seed your database / Create source database state that will be used in each test case (if needed)
// ...
prismaTestingHelper = new PrismaTestingHelper(originalPrismaService);
// Save prismaService. All calls to this prismaService will be routed to the currently active transaction
prismaService = prismaTestingHelper.getProxyClient();
}
await prismaTestingHelper.startNewTransaction();
}
// This function must be called after every test
function after(): void {
prismaTestingHelper?.rollbackCurrentTransaction();
}
// NestJS specific code: Replace the original PrismaService when creating a testing module
// Note that it is possible to cache this result and use the same module for all tests. The prismaService will automatically route all calls to the currently active transaction
function getMockRootModuleBuilder(): TestingModuleBuilder {
return Test.createTestingModule({
imports: [AppModule],
}).overrideProvider(PrismaService)
.useValue(prismaService);
}
The PrismaTestingHelper
provides a proxy to the prisma client and manages this proxy.
Create a single PrismaTestingHelper
per test runner (or a single global one if tests are executed sequentially).
The constructor parameter is the original PrismaClient
that will be used to start transaction.
Note that it is possible to use any objects that extend the PrismaClient, e.g. a NestJS PrismaService.
All methods that don't exist on the prisma transaction client will be routed to this original object (except for $transaction
calls).
This method returns a Proxy to the PrismaClient that will execute all calls inside a transaction. You can save and cache this reference, all calls will always be executed inside the newest transaction. This allows you to e.g. start your application once with the ProxyClient instead of the normal client and then execute all test cases, and all calls will always be routed to the newest transaction.
It is usually enough to fetch this once and then use this reference everywhere.
Starts a new transaction. Must be called before each test (and should be called before any query on the proxy client is executed).
You can provide timeout values - note that the transaction timeout
must be long enough so that your
whole test case will be executed during this timeout.
You must call rollbackCurrentTransaction
before calling this method again.
Ends the currently active transaction. Must be called after each test so that a new transaction can be started.
@default(now())
in your schema (e.g. for a createdAt
date) or similar functionality (e.g. CURRENT_TIMESTAMP()
in PostgreSQL) will always use the start of transaction timestamp. Therefore, all createdAt
-timestamps will have the same value during a test (because they are executed in the same transaction). If this behavior is problematic (e.g. because you want to find the latest entry by creation date), then you can use @default(dbgenerated("statement_timestamp()"))
instead of @default(now())
(if you do not rely on the default "createdAt
= time at start of transaction instead of statement" behavior)await Promise.all(/* Multiple calls that will each start transactions */);
), then they will
automatically be executed sequentially (otherwise, Savepoints wouldn't work). You do not have to change your code for this to work.1.3.1 - 2025-01-23
this
context when calling Prisma functions
Prisma.getExtensionContext(this)
to be undefined
when writing custom Prisma client extensionsFAQs
Provides an easy way to execute database tests in a transaction that will be rolled back after each test for fast testing
The npm package @chax-at/transactional-prisma-testing receives a total of 3,392 weekly downloads. As such, @chax-at/transactional-prisma-testing popularity was classified as popular.
We found that @chax-at/transactional-prisma-testing demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket uncovered four malicious npm packages that exfiltrate up to 85% of a victim’s Ethereum or BSC wallet using obfuscated JavaScript.
Security News
TC39 advances 9 JavaScript proposals, including Array.fromAsync, Error.isError, and Explicit Resource Management, which are now headed into the ECMAScript spec.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.