subzerocloud / postgrest-starter-kit

Starter Kit and tooling for authoring REST API backends with PostgREST
MIT License
744 stars 71 forks source link

CircleCI build fails - 'import' statement unsupported #51

Closed trafgals closed 5 years ago

trafgals commented 5 years ago

I'm able to test using npm test successfully on my local computer running OSX following the tutorial. However when I go to run the same tests in CircleCI, I get the below error log:

#!/bin/bash -eo pipefail
npm test

> starter-kit@ test /home/circleci/project
> npm run test_db && npm run test_rest

> starter-kit@ test_db /home/circleci/project
> ( set -a && . ./.env && set +a && docker run -i -t --rm --name pgtap --net ${COMPOSE_PROJECT_NAME}_default --link ${COMPOSE_PROJECT_NAME}_db_1:db -v $(pwd)/tests/db/:/test -e HOST=$DB_HOST -e DATABASE=$DB_NAME -e USER=$SUPER_USER -e PASSWORD=$SUPER_USER_PASSWORD lren/pgtap )

^@^@Waiting for database...
2019/02/28 03:09:01 Waiting for: tcp://db:5432
2019/02/28 03:09:01 Connected to tcp://db:5432

Running tests: /test/*.sql
/test/rls.sql ........ ok
/test/simple.sql ..... ok
/test/structure.sql .. ok
All tests successful.
Files=3, Tests=13,  0 wallclock secs ( 0.03 usr +  0.01 sys =  0.04 CPU)
Result: PASS

> starter-kit@ test_rest /home/circleci/project
> mocha --compilers js:babel-core/register ./tests/rest/

/home/circleci/project/tests/rest/auth.js:1
(function (exports, require, module, __filename, __dirname) { import { rest_service, jwt, resetdb } from './common.js';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:511:25)
    at loader (/home/circleci/project/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/circleci/project/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at /home/circleci/project/node_modules/mocha/lib/mocha.js:231:27
    at Array.forEach (native)
    at Mocha.loadFiles (/home/circleci/project/node_modules/mocha/lib/mocha.js:228:14)
    at Mocha.run (/home/circleci/project/node_modules/mocha/lib/mocha.js:514:10)
    at Object.<anonymous> (/home/circleci/project/node_modules/mocha/bin/_mocha:480:18)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:445:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! starter-kit@ test_rest: `mocha --compilers js:babel-core/register ./tests/rest/`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the starter-kit@ test_rest script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/circleci/.npm/_logs/2019-02-28T03_09_02_947Z-debug.log
npm ERR! Test failed.  See above for more details.
Exited with code 1

Searching around, it looks like others have run into it using when using Mocha.

ruslantalpa commented 5 years ago

there was something similar in the past, check if this https://github.com/subzerocloud/postgrest-starter-kit/issues/17 and https://docs.subzero.cloud/build-and-deploy-pipeline/ https://docs.subzero.cloud/circleci/config.yml helps

trafgals commented 5 years ago

17 proposed that by pinning the Node version in CircleCI, things would work.

I tried adding this to package.json:

   "engines": {
    "node": "=11.10.0",
    "npm": "=6.7.0"
  },

but no good! Same error still occurs.

This is my first foray into JS, so I'm pretty lost.

ruslantalpa commented 5 years ago

strange because I am doing the tests in some of my circleci configs and I am using the latest node image.

here is a sample (lots of private lines removed but you should get the idea of how I am doing it)

version: 2
jobs:
  pull_docker_images:
    docker:
      - image: circleci/node:latest
    steps:
      - restore_cache:
          keys:
          - v5-docker-images
      - setup_remote_docker
      - run:
          name: docker pull & save images for testing
          command: |            
            if [ ! -f ~/.docker/images.tar ]; then
              mkdir -p ~/.docker
              docker pull postgres
              docker pull lren/pgtap
              docker pull subzerocloud/postgrest
              docker pull openresty/openresty:jessie
              docker pull subzerocloud/amqptools
              docker save --output ~/.docker/images.tar \
                postgres \
                subzerocloud/postgrest \
                lren/pgtap \
                openresty/openresty:jessie \
                subzerocloud/amqptools
            fi
      - save_cache:
          paths:
            - ~/.docker
          key: v5-docker-images

  install_npm_dependencies:
    docker:
      - image: circleci/node:latest
    steps:
      - checkout
      - restore_cache:
          keys:
          - v2-npm-dependencies-{{ checksum "package.json" }}
      - run: npm install
      - save_cache:
          paths:
            - node_modules
          key: v2-npm-dependencies-{{ checksum "package.json" }}
  test:
    machine:
      enabled: true
    steps:
      - checkout
      - restore_cache:
          keys:
          - v5-docker-images
      - restore_cache:
          keys:
          - v2-npm-dependencies-{{ checksum "package.json" }}
      - run:
          name: load cached docker images
          command: |
            docker load --input ~/.docker/images.tar
      - run:
         name: bring up application stack
         command: |
            # echo manual | sudo tee /etc/init/postgresql.override
            # sudo service postgresql stop
            # while sudo lsof -Pi :5432 -sTCP:LISTEN -t; do sleep 1; done
            docker-compose up -d db postgrest openresty
            sleep 10
      - run:
          name: run tests
          command: npm test

  deploy:
    machine:
      enabled: true
    steps:
      - checkout

      - deploy:
          name: login to aws docker repo
          command: aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION | sh

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - pull_docker_images:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      - install_npm_dependencies:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      - test:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
          requires:
            - pull_docker_images
            - install_npm_dependencies
      - deploy:
          requires:
            - test
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
trafgals commented 5 years ago

I don't understand what you changed but using the above it works now, whereas the one linked to by the subzero docs doesn't work. You could add the config to the repository, so users can fork and get successful tests immediately. :)

ruslantalpa commented 5 years ago

can you share the previous config that failed and it's log output and this new one that worked along with it's load output? I want to pinpoint the difference and understand what's going on

ruslantalpa commented 5 years ago

could it be that somehow the config was goo but you had "bad cache" and you just needed to clear it (and it got cleared now because in the new config I used some other name to refer to the cache)?