简体   繁体   中英

TypeError: undefined is not a function with karma test but I get the expected result from the function itself

Here is my function:

    const retrieve = (options = {page: 1, colors: []}) => {
        const limit = 10;
        var page = options.page || 1;
        const offset = (page * limit)-10;
        let color = options.colors || [];
        if (color.length === 1) {
             color[1] = "fjkdow";
        }
        if (page > 1) {
            output.previousPage = page -1;
        } else {
            output.previousPage = null;
        }
        if (page <50){
            output.nextPage = page + 1;
        } else {
            output.nextPage =  null;
        }
        //(limit=5, offset=0, color=['brown', 'green']);
        var template = URI(window.path).query({limit: limit, offset: offset, 
        color: color})
       fetch(template)
       .then(function(response) {
           if (response.status === 404) {
           console.log('404 not found')
        }
        return response.json()
        }).then(function(json) {
            isPrimary(json)
            getIds(json)
            getOpen(json)
            getClosed(json)
            if(json.length === 0 && page === 1) {
                output.nextPage =  null;
            }
        }).catch(function(ex) {
            console.log('parsing failed')
        })
        return output
    }

    export default retrieve;

The function works and my output to the console matches the expected result. I was given a test file to run on the function. When I run the test, I get the error:

TypeError: undefined is not a function (evaluating '(0, _managedRecords2.default)().then(check)') in test/api/managed-record-test.js (line 149) webpack:///test/api/managed-record-test.js:91:20 <- test/api/managed-record-test.js:149:42

The test looks like this:

    describe("Records", function() {

      it('should return unfiltered results', function(done){
        var expected = {"previousPage":null,"nextPage":2,
         "ids": [1,2,3,4,5,6,7,8,9,10],"open":  
         [{"id":2,"color":"yellow","disposition":"open","isPrimary":true},
          {"id":4,"color":"brown","disposition":"open","isPrimary":false},
          {"id":6,"color":"blue","disposition":"open","isPrimary":true},
          {"id":8,"color":"green","disposition":"open","isPrimary":false},
          {"id":10,"color":"red","disposition":"open","isPrimary":true}],
          "closedPrimaryCount":1};
        retrieve().then(function(output){
          expect(output).toEqual(expected);
        }).then(done);
      });
    });

Here are the dependencies and devDependencies from the package.json

    "dependencies": {
      "babel-core": "^6.21.0",
      "babel-loader": "6.4.0",
      "babel-polyfill": "^6.20.0",
      "babel-preset-env": "^1.4.0",
      "es6-promise": "^4.0.5",
      "express": "^4.15.2",
      "fetch-ponyfill": "^3.0.2",
      "karma-sourcemap-loader": "^0.3.7",
      "urijs": "^1.18.4"
    },
    "devDependencies": {
      "jasmine-core": "^2.4.1",
      "karma": "^1.7.0",
      "karma-chrome-launcher": "^0.2.2",
      "karma-jasmine": "^0.3.0",
      "karma-phantomjs-launcher": "^1.0.0",
      "karma-webpack": "^1.7.0",
      "phantomjs-prebuilt": "^2.1.7",
      "webpack": "^1.12.14"
    }

Can someone please help me understand why the test keeps failing and how to fix it? I have run with both the PhantomJS and Chrome browsers and I get the same error.

因此,事实证明我的测试正在寻找对象的承诺,而我正在返回实际对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM