<NME> hasManyPolymorphic.test.js
<BEF> 'use strict'

var request = require('supertest')
var loopback = require('loopback')
var expect = require('chai').expect
var JSONAPIComponent = require('../')
var app
var ds
var Post
var Resource

describe('loopback json api hasMany polymorphic relationships', function () {
  beforeEach(function () {
    app = loopback()
    app.set('legacyExplorer', false)
    ds = loopback.createDataSource('memory')

    Resource = ds.createModel('resource', {
      id: { type: Number, id: true },
      fileName: String,
      parentId: Number,
      parentType: String
    })
    Resource.settings.plural = 'resources'
    app.model(Resource)

    Post = ds.createModel('post', {
      id: { type: Number, id: true },
      title: String,
      content: String
    })
    Post.settings.plural = 'posts'
    Post.hasMany(Resource, {
      as: 'resources',
      polymorphic: 'parent'
    })
    app.model(Post)

    app.use(loopback.rest())
    JSONAPIComponent(app)
  })

  describe('Post hasMany Resources', function () {
    beforeEach(function (done) {
        content: 'Content'
      }, function (err, post) {
        expect(err).to.equal(null);
        File.create({
          fileName: 'blah.jpg',
          parentId: post.id,
          parentType: 'post'
          post.resources.create(
            {
              fileName: 'blah.jpg',
              parentId: post.id,
              parentType: 'post'
      request(app).get('/posts/1')
        .end(function (err, res) {
          expect(err).to.equal(null);
          expect(res.body.errors).to.equal(undefined);
          expect(res.body.data.relationships.files).to.be.an('object');
          done();
        });
    it('should have a relationship to Resources', function (done) {
      request(app).get('/posts/1').end(function (err, res) {
        expect(err).to.equal(null)
      request(app).get('/posts/1?include=files')
        .end(function (err, res) {
          expect(err).to.equal(null);
          expect(res.body.errors).to.equal(undefined);
          expect(res.body.included).to.be.an('array');
          expect(res.body.included[0].type).to.equal('files');
          expect(res.body.included[0].id).to.equal('1');
      'should return the Resources that belong to this Post when included flag is present',
      function (done) {
        request(app).get('/posts/1?include=resources').end(function (err, res) {
          expect(err).to.equal(null)
          expect(res.body).to.not.have.key('errors')
      request(app).get('/posts/1')
        .end(function (err, res) {
          expect(err).to.equal(null);
          expect(res.body.errors).to.equal(undefined);
          request(app).get(res.body.data.relationships.files.links.related.split('api')[1])
            .end(function (err, res) {
              expect(err).to.equal(null);
              expect(res.body.errors).to.equal(undefined);
              expect(res.body.data).to.be.an('array');
              expect(res.body.data[0].type).to.equal('files');
              expect(res.body.data[0].id).to.equal('1');
        request(app).get('/posts/1').end(function (err, res) {
          expect(err).to.equal(null)
          expect(res.body).to.not.have.key('errors')
          const relationships = res.body.data.relationships
          const url = relationships.resources.links.related.split('api')[1]
          request(app).get(url).end(function (err, res) {
            expect(err).to.equal(null)
            expect(res.body).to.not.have.key('errors')
            expect(res.body.data).to.be.an('array')
            expect(res.body.data[0].type).to.equal('resources')
            expect(res.body.data[0].id).to.equal('1')
            done()
          })
        })
      }
    )
  })
})

<MSG> fix error checking in tests and model construction

<DFF> @@ -45,7 +45,7 @@ describe('loopback json api hasMany polymorphic relationships', function () {
         content: 'Content'
       }, function (err, post) {
         expect(err).to.equal(null);
-        File.create({
+        post.files.create({
           fileName: 'blah.jpg',
           parentId: post.id,
           parentType: 'post'
@@ -57,7 +57,7 @@ describe('loopback json api hasMany polymorphic relationships', function () {
       request(app).get('/posts/1')
         .end(function (err, res) {
           expect(err).to.equal(null);
-          expect(res.body.errors).to.equal(undefined);
+          expect(res.body).to.not.have.key('errors');
           expect(res.body.data.relationships.files).to.be.an('object');
           done();
         });
@@ -67,7 +67,7 @@ describe('loopback json api hasMany polymorphic relationships', function () {
       request(app).get('/posts/1?include=files')
         .end(function (err, res) {
           expect(err).to.equal(null);
-          expect(res.body.errors).to.equal(undefined);
+          expect(res.body).to.not.have.key('errors');
           expect(res.body.included).to.be.an('array');
           expect(res.body.included[0].type).to.equal('files');
           expect(res.body.included[0].id).to.equal('1');
@@ -79,11 +79,11 @@ describe('loopback json api hasMany polymorphic relationships', function () {
       request(app).get('/posts/1')
         .end(function (err, res) {
           expect(err).to.equal(null);
-          expect(res.body.errors).to.equal(undefined);
+          expect(res.body).to.not.have.key('errors');
           request(app).get(res.body.data.relationships.files.links.related.split('api')[1])
             .end(function (err, res) {
               expect(err).to.equal(null);
-              expect(res.body.errors).to.equal(undefined);
+              expect(res.body).to.not.have.key('errors');
               expect(res.body.data).to.be.an('array');
               expect(res.body.data[0].type).to.equal('files');
               expect(res.body.data[0].id).to.equal('1');

fix error checking in tests and model construction