meteor / tutorials

The Meteor tutorials from meteor.com
https://www.meteor.com/tutorials
MIT License
181 stars 103 forks source link

Step 11, testing, use of fat arrow despite Mocha recommendation #87

Open jjblum opened 8 years ago

jjblum commented 8 years ago

The tutorial's tasks.tests.js code utilizes fat arrow functions. The current Mocha documentation (http://mochajs.org/#arrow-functions) explicitly discourages this.

You may want to include alternative test code that does not use arrow functions.

abernix commented 7 years ago

We shouldn't use fat-arrows if they are not recommended by Mocha.

MichaelJCole commented 5 years ago

Fat-arrows break this, and the tutorials don't use this in the testing code.

https://www.meteor.com/tutorials/blaze/testing

This will need a PR for each of the tutorial code repos. The working code (for the Blaze tutorial anyway) is:

1) Create an "imports/api/tasks.tests.js"

/* eslint-env mocha */

import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { assert } from 'meteor/practicalmeteor:chai';

import { Tasks } from '/imports/api/tasks.js';

if (Meteor.isServer) {
  describe('Tasks', function() {
    describe('methods', function() {
      const userId = Random.id();
      let taskId;

      beforeEach(function() {
        Tasks.remove({});
        taskId = Tasks.insert({
          text: 'test task',
          createdAt: new Date(),
          owner: userId,
          username: 'tmeasday',
        });
      });

      it('can delete owned task', function() {
        // Find the internal implementation of the task method so we can
        // test it in isolation
        const deleteTask = Meteor.server.method_handlers['tasks.remove'];

        // Set up a fake method invocation that looks like what the method expects
        const invocation = { userId };

        // Run the method with `this` set to the fake invocation
        deleteTask.apply(invocation, [taskId]);

        // Verify that the method does what we expected
        assert.equal(Tasks.find().count(), 0);
      });
    });
  });
}

2) Add the import to /tests/main.js

import assert from "assert";
import '/imports/api/tasks.tests';

describe("simple-todos", function () {
  it("package.json has correct name", async function () {
    const { name } = await import("../package.json");
    assert.strictEqual(name, "simple-todos");
  });

  if (Meteor.isClient) {
    it("client is not server", function () {
      assert.strictEqual(Meteor.isServer, false);
    });
  }

  if (Meteor.isServer) {
    it("server is not client", function () {
      assert.strictEqual(Meteor.isClient, false);
    });
  }
});

3) Run