Skip to content

Commit

Permalink
feat: run tests on Deno
Browse files Browse the repository at this point in the history
Re: #9056
  • Loading branch information
vkarpov15 committed Sep 4, 2022
1 parent c4f0027 commit 3f79b18
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 4 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -91,6 +91,23 @@ jobs:
with:
name: coverage
path: coverage

test-deno:
runs-on: ubuntu-latest
name: Deno tests
steps:
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.0.2
- name: Setup node
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # v3.4.1
with:
node-version: 16
- name: Setup Deno
uses: actions/setup-deno #v1
with:
deno-version: v1.25.x
- run: npm install
- name: Run Deno tests
run: npm run test-deno

test-replica-sets:
needs:
Expand Down
2 changes: 1 addition & 1 deletion lib/schematype.js
Expand Up @@ -56,7 +56,7 @@ function SchemaType(path, options, instance) {
const defaultOptionsKeys = Object.keys(defaultOptions);

for (const option of defaultOptionsKeys) {
if (defaultOptions.hasOwnProperty(option) && !options.hasOwnProperty(option)) {
if (defaultOptions.hasOwnProperty(option) && !Object.prototype.hasOwnProperty.call(options, option)) {
options[option] = defaultOptions[option];
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -94,6 +94,7 @@
"release-legacy": "git pull origin 5.x && git push origin 5.x --tags && npm publish --tag legacy",
"mongo": "node ./tools/repl.js",
"test": "mocha --exit ./test/*.test.js",
"test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --unstable ./test/deno.js",
"test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
"test-tsd": "node ./test/types/check-types-filename && tsd",
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
Expand Down
33 changes: 33 additions & 0 deletions test/deno.js
@@ -0,0 +1,33 @@
'use strict';

import { createRequire } from "https://deno.land/std/node/module.ts";

import { parse } from "https://deno.land/std/flags/mod.ts"
const args = parse(Deno.args);

const require = createRequire(import.meta.url);

const Mocha = require('mocha');
const fs = require('fs');
const path = require('path');

const mocha = new Mocha({
...(args.g ? { fgrep: '' + args.g } : {})
});

const testDir = 'test';
const files = fs.readdirSync(testDir);

const ignoreFiles = new Set(['browser.test.js', 'connection.test.js', 'index.test.js']);

for (const file of files) {
if (!file.endsWith('.test.js') || ignoreFiles.has(file)) {
continue;
}

mocha.addFile(path.join(testDir, file));
}

mocha.run(function(failures) {
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
});
7 changes: 6 additions & 1 deletion test/docs/lean.test.js
@@ -1,7 +1,6 @@
'use strict';

const assert = require('assert');
const v8Serialize = require('v8').serialize;
const start = require('../common');

// This file is in `es-next` because it uses async/await for convenience
Expand All @@ -23,6 +22,12 @@ describe('Lean Tutorial', function() {
});

it('compare sizes lean vs not lean', async function() {
// acquit:ignore:start
if (typeof Deno !== 'undefined') {
return this.skip(); // Deno does not support v8.serialize()
}
const v8Serialize = require('v8').serialize;
// acquit:ignore:end
const schema = new mongoose.Schema({ name: String });
const MyModel = mongoose.model('Test', schema);

Expand Down
7 changes: 7 additions & 0 deletions test/query.test.js
Expand Up @@ -2339,6 +2339,13 @@ describe('Query', function() {
});

it('throw on sync exceptions in callbacks (gh-6178)', function(done) {
// Deno doesn't support 'uncaughtException', so there's no way to test this in Deno
// without starting a separate process.
// See: https://stackoverflow.com/questions/64871554/deno-how-to-handle-exceptions
if (typeof Deno !== 'undefined') {
return this.skip();
}

const schema = new Schema({});
const Test = db.model('Test', schema);

Expand Down
3 changes: 1 addition & 2 deletions test/schema.subdocumentpath.test.js
Expand Up @@ -179,7 +179,7 @@ describe('SubdocumentPath', function() {
assert.ok(err);
assert.ok(err.errors['nested']);

mongoose.Schema.Types.Subdocument.set('required', false);
delete mongoose.Schema.Types.Subdocument.defaultOptions.required;
});

it('supports setting _id globally (gh-11541) (gh-8883)', function() {
Expand All @@ -197,6 +197,5 @@ describe('SubdocumentPath', function() {
assert.ok(!doc.nested._id);

delete mongoose.Schema.Types.Subdocument.defaultOptions._id;
mongoose.Schema.Types.Subdocument.set('required', false);
});
});
5 changes: 5 additions & 0 deletions test/schema.test.js
Expand Up @@ -1178,6 +1178,11 @@ describe('schema', function() {

describe('other contexts', function() {
it('work', function(done) {
if (typeof Deno !== 'undefined') {
// Deno throws "Not implemented: Script.prototype.runInNewContext"
return this.skip();
}

const str = 'code = {' +
' name: String' +
', arr1: Array ' +
Expand Down
2 changes: 2 additions & 0 deletions test/schematype.test.js
Expand Up @@ -224,6 +224,8 @@ describe('schematype', function() {

// Assert
assert.equal(schema.path('test').options.someRandomOption, true);

delete type.defaultOptions.someRandomOption;
});
});
});
Expand Down

0 comments on commit 3f79b18

Please sign in to comment.