I’m using typescript in a application and created a test to populate a database using Prisma ORM.
I have a function that returns a string.
const mappingPayload = (data: any) => {
let pay = []
const payload = data
pay.push('${payload}')
return pay
}
The test looks like this in the beginning:
import {data, mappingPayload } from '../../src/taskHandlers/buildPayload';
describe('Build Payload', () => {
beforeAll(
async () => {
await loadConfig();
const db = await getDbClient()
const dataJir =mappingPayload(data)
payload = await db.cadPayload.create({ data: {
"resource_type": "mixture",
"number_of_protected_ips": [1, 8],
"total_inbound_traffic": [0, 50],
"total_mitigated_traffic": 20,
"resources_behind": true,
"package": "alwayson",}
});
This works as I’m passing raw data straight into the DB.Create object.
However I’d like to pass the function result string into the DB.Create object.
import {data, mappingPayload } from '../../src/taskHandlers/buildPayload';
describe('Build Payload', () => {
beforeAll(
async () => {
await loadConfig();
const db = await getDbClient()
const dataJir =mappingPayload(data) //FROM HERE TO-->
payload = await db.cadPayload.create({ data: { dataJir } }); //HERE!
As you can see I’m passing the returning value of the mapping function to the database.
But keep getting this ERROR from my actual test case:
➜ testing git:(pnyeko_feature_pi_CAD_17_3.1) ✗ npm run test buildPayload.test.ts
> [email protected] test
> jest buildPayload.test.ts
FAIL testing/integration/buildPayload.test.ts
● Test suite failed to run
testing/integration/buildPayload.test.ts:26:60 - error TS2353: Object literal may only specify known properties, and 'dataJir' does not exist in type '(Without<cadPayloadCreateInput, cadPayloadUncheckedCreateInput> & cadPayloadUncheckedCreateInput) | (Without<...> & cadPayloadCreateInput)'.
26 payload = await db.cadPayload.create({ data: { dataJir
~~~~~~~
node_modules/.prisma/client/index.d.ts:10977:5
10977 data: XOR<cadPayloadCreateInput, cadPayloadUncheckedCreateInput>
~~~~
The expected type comes from property 'data' which is declared here on type '{ select?: cadPayloadSelect | null | undefined; data: (Without<cadPayloadCreateInput, cadPayloadUncheckedCreateInput> & cadPayloadUncheckedCreateInput) | (Without<...> & cadPayloadCreateInput); }'
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 6.733 s, estimated 9 s
Ran all test suites matching /buildPayload.test.ts/i.
How does one pass the result of a function that returns a string, into a prisma create object as if it were raw data?
Patrick Nyeko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.