Hello.
When I try to do the test I recive this error mesage:
C:\Users\pablo\Desktop\DAM2\mflix-js>npm test -t migration
server@1.0.0 test
jest --passWithNoTests “migration”
Determining test suites to run…Setup Mongo Connection
(node:3920) Warning: Accessing non-existent property ‘count’ of module exports inside circular dependency
(Use node --trace-warnings ...
to show where the warning was created)
(node:3920) Warning: Accessing non-existent property ‘findOne’ of module exports inside circular dependency
(node:3920) Warning: Accessing non-existent property ‘remove’ of module exports inside circular dependency
(node:3920) Warning: Accessing non-existent property ‘updateOne’ of module exports inside circular dependency
(node:3920) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to MongoClient.connect.
FAIL test/migration.test.js (30.969s)
Migration
× migration (48ms)
● Migration › migration
expect(received).not.toBeNull()
Received: null
12 | lastupdated: { $type: "date" },
13 | })
> 14 | expect(movie).not.toBeNull()
| ^
15 | })
16 | })
17 |
at _callee2$ (test/migration.test.js:14:23)
at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
at Generator.prototype.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 74.313s
Ran all test suites matching /migration/i.
Teardown Mongo Connection
The code that I have is :
const MongoClient = require(“mongodb”).MongoClient
const ObjectId = require(“mongodb”).ObjectId
const MongoError = require(“mongodb”).MongoError
require(“dotenv”).config()
/**
-
Ticket: Migration
-
Update all the documents in the
movies
collection, such that the -
“lastupdated” field is stored as an ISODate() rather than a string.
-
The Date.parse() method build into Javascript will prove very useful here!
-
Refer to CRUD Operations
*/
// This leading semicolon ( is to signify to the parser that this is a new expression. This expression is an
// Immediately Invoked Function Expression (IIFE). It’s being used to wrap this logic in an asynchronous function
// so we can use await within.
// To read more about this type of expression, refer to IIFE - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
;(async () => {
try {
const host = process.env.MFLIX_DB_URI
const client = await MongoClient.connect(host, { useNewUrlParser: true })
const mflix = client.db(process.env.MFLIX_NS)
// TODO: Create the proper predicate and projection
// add a predicate that checks that the `lastupdated` field exists, and then
// check that its type is a string
// a projection is not required, but may help reduce the amount of data sent
// over the wire!
const predicate = { "lastupdated": { $exists: true } , "lastupdated": { $type: "string" } }
const projection = {"lastupdated": 1, _id:1}
const cursor = await mflix
.collection("movies")
.find(predicate, projection)
.toArray()
const moviesToMigrate = cursor.map(({ _id, lastupdated }) => ({
updateOne: {
filter: { _id: ObjectId(_id) },
update: {
$set: { lastupdated: Date.parse(lastupdated) },
},
},
}))
console.log(
"\x1b[32m",
`Found ${moviesToMigrate.length} documents to update`,
)
// TODO: Complete the BulkWrite statement below
const { modifiedCount } = await mflix.collection("movies").bulkWrite([moviesToMigrate],{ordered: false})
console.log("\x1b[32m", `${modifiedCount} documents updated`)
client.close()
process.exit(0)
} catch (e) {
if (
e instanceof MongoError &&
e.message.slice(0, "Invalid Operation".length) === "Invalid Operation"
) {
console.log("\x1b[32m", "No documents to update")
} else {
console.error("\x1b[31m", `Error during migration, ${e}`)
}
process.exit(1)
}
})()
I don´t have idea why I´m receiving this error.
Any help will be well received.