Ошибка перекрестного происхождения http://localhost при тестировании асин c сервисов с использованием приложений и приложений Jact Create - PullRequest
0 голосов
/ 25 февраля 2020

Попытка проверить мои услуги, но получение перекрестного происхождения http://localhost Запрещенная ошибка. Я не уверен, имеет ли это отношение к моему authHeader, который я не передаю. Должен ли я передавать свой токен JWT вместе с тестом? когда я консоль регистрирую сервис и запускаю тест, я вижу, что мой токен присутствует.

Вот мой тест

import React from 'react'
import { userService } from './user-service'

let user

beforeAll(async() => {
  const credentials = {
    email: 'example@example.com',
    password: 'somepassword'
  }

  user = await userService.login(credentials)
})

test('check res after login', async() => {
  expect(user.name).toBe('example')
  expect(user.token).toBeDefined()
})

test('get user profile', async() => {
  const userProfile = await userService.getProfile()
  console.log('profile')

  expect(userProfile.name).toBeDefined()
  expect(userProfile.language).toBeDefined()
})

test('create a new user', async() => {
  const userBody = {
    confirmPassword: "password",
    email: "test@iamtesting.com",
    name: "test",
    password: "password",
    phone: "999",
    roleName: "default",
  }

  const createUser = await userService.create(userBody)

  expect(createUser.name).toBe('test')
  expect(createUser.phone).toBe('999')
})

и мой сервисный код

function login(credentials) {
  const requestOptions = {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(credentials)
  };

  return fetch(`${process.env.REACT_APP_API}/login`, requestOptions)
    .then(handleResponse)
    .then(user => {
      // login successful if there's a jwt token in the response
      if (user.token) {
        // store user details and jwt token in local storage to keep user logged in between page refrehes
          localStorage.setItem('user', JSON.stringify(user));
      }

      return user;
    });
}
function getProfile() {
  const requestOptions = {
    method: 'GET',
    headers: authHeader()
  }

  return fetch(`${getBackend()}/user?profile=true`, requestOptions).then(handleResponse); 
}

function create(user) {
  const requestOptions = {
    method: 'POST',
    headers: authHeader(),
    body: JSON.stringify(user)
  }

  return fetch(`${getBackend()}/user`, requestOptions).then(handleResponse); 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...